Last active
February 24, 2023 15:31
-
-
Save lukehoban/5c168258b641368dcccc7810dc454ca9 to your computer and use it in GitHub Desktop.
Simple Aurora Serverless + Lambda VPC example with Pulumi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as pulumi from "@pulumi/pulumi"; | |
import * as aws from "@pulumi/aws"; | |
import * as awsx from "@pulumi/awsx"; | |
import * as random from "@pulumi/random"; | |
// Construct a VPC | |
const vpc = new awsx.ec2.Vpc("vpc"); | |
// Create an Aurora Serverless MySQL database | |
const dbsubnet = new aws.rds.SubnetGroup("dbsubnet", { | |
subnetIds: vpc.privateSubnetIds, | |
}); | |
const dbpassword = new random.RandomString("password", { | |
length: 20, | |
}); | |
const db = new aws.rds.Cluster("db", { | |
engine: "aurora", | |
engineMode: "serverless", | |
engineVersion: "5.6.10a", | |
dbSubnetGroupName: dbsubnet.name, | |
masterUsername: "pulumi", | |
masterPassword: dbpassword.result, | |
}); | |
// A function to run to connect to our database. | |
function queryDatabase(): Promise<void> { | |
return new Promise((resolve, reject) => { | |
var mysql = require('mysql'); | |
var connection = mysql.createConnection({ | |
host : db.endpoint.get(), | |
user : db.masterUsername.get(), | |
password : db.masterPassword.get(), | |
database : db.databaseName.get(), | |
}); | |
connection.connect(); | |
console.log("querying...") | |
connection.query('SELECT 1 + 1 AS solution', function (error: any, results: any, fields: any) { | |
if (error) { reject(error); return } | |
console.log('The solution is: ', results[0].solution); | |
resolve(); | |
}); | |
connection.end(); | |
}); | |
} | |
// Create a Lambda within the VPC to access the Aurora DB and run the code above. | |
const lambda = new aws.lambda.CallbackFunction("lambda", { | |
vpcConfig: { | |
securityGroupIds: db.vpcSecurityGroupIds, | |
subnetIds: vpc.privateSubnetIds, | |
}, | |
policies: [aws.iam.AWSLambdaVPCAccessExecutionRole, aws.iam.AWSLambdaFullAccess, aws.iam.AmazonRDSFullAccess], | |
callback: async(ev) => { | |
console.log(ev); | |
await queryDatabase(); | |
}, | |
}); | |
// Export the Function ARN | |
export const functionArn = lambda.arn; | |
// Invoke this with: | |
// $ aws lambda invoke --function-name $(pulumi stack output functionArn) out.txt | |
// $ pulumi logs -f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Luke, I must have missed a step. Any chance you know how to resolve the following MODULE_NOT_FOUND runtime error that is reported during the execution of the Lambda function? It appears that the mysql node module is not available from within the AWS Lambda runtime environment.
PS: I've used the above code while following the related YouTube live coding session: https://www.youtube.com/watch?v=yMhJgkKCQGM
$ pulumi version
v3.28.0
PS C:> cmd ?
Microsoft Windows [Version 10.0.19043.1586]
(c) Microsoft Corporation. All rights reserved.
$ npm -v mysql
6.14.15
$ npm -v
6.14.15
$ node -v
v14.18.0