0

How can I move the DB Configuration and schema creation from Terraform into AWS Lambda, and then create a cloudwatch event which can invoke the Lambda to perform the operation.

This operation can be creating the connection, schema and then running some alembic scripts.

For example:

    provider "postgresql" {
  host     = "127.0.0.1"
  database = "${var.db1_name}"
  username = "my_role"
  password = "foobar"
  sslmode  = "disable"
}

resource "postgresql_schema" "my_schema1" {
  name = "my_schema"
}

If my schema.tf file looks like this,

How can these moved to lambda.py and package them together as a zip? Any help is appreciated, thank you!

2
  • Basically you are asking for an entire Lambda Function Code Commented Apr 7, 2022 at 11:03
  • Not entire code :) !! Commented Apr 7, 2022 at 14:42

1 Answer 1

1

Create RDS Instance using boto3

import boto3

client = boto3.client('rds')

response = conn.create_db_instance(
        AllocatedStorage=10,
        DBName="test",
        DBInstanceIdentifier="my-first-rds-instance",
        DBInstanceClass="db.t2.micro",
        Engine="mysql",
        MasterUsername="root",
        MasterUserPassword="pass1234",
        Port=3306,
        VpcSecurityGroupIds=["sg-7fa4d512"],
    )

print (response)

If you want to configure other parameters for RDS you can have a look here.

Create CloudWatch Event:

import boto3

AWS_REGION = "us-west-2"

client = boto3.client('events', region_name=AWS_REGION)

response = cloudwatch.put_rule(
    Name='AlarmStateChange',
    EventPattern='{"source": ["aws.cloudwatch"], "detail-type": ["CloudWatch Alarm State Change"]}',
    State='ENABLED'
)

response = cloudwatch.put_targets(
    Rule='AlarmStateChange',
    Targets=[
        {
            'Arn': 'arn:aws:lambda:us-west-2:585584209241:function:DemoFunction',
            'Id': 'myCloudWatchEventsTarget',
        }
    ]
)

print(response)

If you want to configure other parameters for CW you can have a look here.

For Operation, you will have to create another Lambda Function and add the necessary code.

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Turns out, I figured this and it's helpful. But I was wondering about the automation of migration scripts adjacent to the lambda. Currently I use alembic upgrade and has a script for it. Is there an easy way to keep it adjacent to the lambda. Means once I create the connection, I could run the migration scripts from my AWS Lambda. Any pointer is helpful too. Thank you @Shivkumar Mallesappa
You can send a notification to SNS or send a message to SQS, and upon that event, you can trigger multiple Lambdas. Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.