You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

cloudformation-sql-run

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cloudformation-sql-run

Cloudformation resource to call any http API

2.1.6
latest
npmnpm
Version published
Weekly downloads
213
53.24%
Maintainers
1
Weekly downloads
 
Created
Source

Call SQL during CloudFormation update

An aws-cdk construct for calling SQL during CloudFormation stack update.

Usage

The SqlRun resource provides 2 callbacks: up, down.

The up callback runs a forward migration. The down command should do the opposite of up.

The following example will issue a POST request when a Some API resource creates:

import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as rds from "aws-cdk-lib/aws-rds";
import {
  DatabaseInstanceEngine,
  PostgresEngineVersion
} from "aws-cdk-lib/aws-rds";
import * as secretmanager from 'aws-cdk-lib/aws-secretsmanager';
import * as cdk from "aws-cdk-lib";
import { RemovalPolicy } from "aws-cdk-lib";
import { SqlRun, SqlRunConnection, SqlSecret } from "cloudformation-sql-run";
import { Construct } from "constructs";

export class ExamplesStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const password = new secretmanager.Secret(this, 'Some Password')

    const vpc = new ec2.Vpc(this, 'vpc', {
      natGateways: 1
    })

    const db = new rds.DatabaseInstance(this, 'db', {
      databaseName: 'sqlrunexample',
      engine: DatabaseInstanceEngine.postgres({
        version: PostgresEngineVersion.VER_10
      }),
      vpc: vpc,
      removalPolicy: RemovalPolicy.DESTROY
    })

    const createDatabaseUser = new SqlRun(this, 'Create Database User', {
      vpc: vpc,
      connection: SqlRunConnection.fromDatabaseInstance(db),
      up: {
        run: [{
          sql: `CREATE TABLE items(name varchar)`
        }, {
          sql: `INSERT INTO items(name) VALUE (:secret)`,
          parameters: {
            secret: SqlSecret.fromSecretsManager(password)
          }
        }],
      },
      down: {
        run: [{
          sql: `DROP TABLE items`
        }]
      }
    });
  }
}

SQL results as data

You can access results of the last sql statement as follows:

const createDatabaseUser = new SqlRun(this, 'Create Database User', {
  up: { run: [{ sql: `select count(*) as admin_count, company_id from users where is_admin = true group by company_id` }] }
});

new CfnOutput(this, 'Row at index 0', {
  description: "First result",
  value: sampleQuery.getResultField("[0].admin_count")
})

new CfnOutput(this, 'Row at index 1', {
  description: "Second result",
  value: sampleQuery.getResultField("[1].company_id")
})

Keywords

cdk

FAQs

Package last updated on 23 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts