SQL using Cloud formation

0

I have some stored procs and sql statements to run in rds immediately after its creation using cloud formation. Is there an option to run those queries using cloud formation.

2 Answers
1

Hello,

You can use CloudFormation custom resource to initialize rds in cloudformation, this repository is an example that may help you:

https://github.com/iann0036/pg-init-custom-resource

answered 2 months ago
profile picture
EXPERT
Sandeep
reviewed 2 months ago
0

Steps to Execute SQL Queries After RDS Creation with CloudFormation

Create the RDS Instance using CloudFormation:

In your CloudFormation template, define the RDS instance and other necessary resources like security groups, subnets, etc.

Create a Lambda Function:

This Lambda function will execute the SQL queries on the RDS instance after it is created. The function can be triggered either by a CloudWatch Event when the RDS instance is ready or by AWS Systems Manager after CloudFormation completes.

Create an SSM Document:

Store your SQL statements or stored procedures in an SSM Document. This allows you to pass the SQL commands securely and execute them via Lambda.

Add a CloudWatch Event Rule or SSM Automation:

Set up a CloudWatch Event Rule that triggers the Lambda function after the RDS instance is created and becomes available. Alternatively, you can use an SSM Automation document to trigger the Lambda function after the CloudFormation stack is complete.

IAM Roles and Permissions:

Ensure that the Lambda function has the necessary IAM permissions to execute queries on the RDS instance and to access the RDS instance credentials stored in AWS Secrets Manager (if applicable).

Example CloudFormation Template Snippet

Here’s a basic example to illustrate how you might set this up in CloudFormation:

Resources:
  MyDBInstance:
   

EXPERT
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions