Anupam Dixit
08/23/2021, 11:42 AMRoss Coundon
08/23/2021, 12:05 PMexport default function main(app: <http://sst.App|sst.App>): void {
const vpcsByStackName: VpcsByStackName = {};
const securityGroupsByStackName: SecGroupsByStackName = {};
const vpcsByStackName: VpcsByStackName = {};
const securityGroupsByStackName: SecGroupsByStackName = {};
app.setDefaultFunctionProps((stack) => {
// Get already imported VPC and SG
let vpc = vpcsByStackName[stack.stackName];
let securityGroup = securityGroupsByStackName[stack.stackName];
// If VPC and SG have not been imported, then import now
if (!vpc) {
vpc = Vpc.fromLookup(stack, 'default-vpc', {
isDefault: true,
});
if (process.env.VPC_RDS_SECURITY_GROUP) {
securityGroup = SecurityGroup.fromSecurityGroupId(stack, 'default-sg', process.env.VPC_RDS_SECURITY_GROUP);
securityGroupsByStackName[stack.stackName] = securityGroup;
}
vpcsByStackName[stack.stackName] = vpc;
}
// Return the function definition{
const props: DefaultProps = {
runtime: 'nodejs14.x',
vpc,
};
if (securityGroup) {
props.securityGroups = [securityGroup];
}
return props;
});
new OmwOfscBeStack(app, `OmwOfscBeStack-${stage}`);
}
Anupam Dixit
08/23/2021, 12:45 PMconst vpc = ec2.Vpc.fromLookup(this, 'qa-cluster-eks', {});
const api = new Api(stack, 'Api', {
defaultFunctionProps: {
environment: options.defaultEnvironment || {},
permissions: [options.commonQueue],
vpc,
securityGroup,
},
routes: {
'GET /health': getFunctionDef('health', scope, true), // Lite handler
},
});
Ross Coundon
08/23/2021, 12:46 PMAnupam Dixit
08/23/2021, 1:10 PMRoss Coundon
08/23/2021, 1:17 PMvpc = Vpc.fromLookup(stack, 'default-vpc', {
isDefault: true,
});
const props = {
runtime: 'nodejs14.x',
vpc: {
publicSubnets: vpc.publicSubnets
}
};
Anupam Dixit
08/23/2021, 1:18 PMRoss Coundon
08/23/2021, 1:19 PMWilliam Hatch
10/14/2021, 10:43 PMSeth Geoghegan
10/18/2021, 3:03 PMRoss Coundon
10/18/2021, 3:09 PMSeth Geoghegan
10/18/2021, 3:11 PMWilliam Hatch
10/18/2021, 6:11 PMSeth Geoghegan
10/18/2021, 8:22 PMSeth Geoghegan
10/18/2021, 8:26 PMimport json
import psycopg2
db_host = "MY_HOST_INSIDE_THE_VPC"
db_port = "DB_PORT"
db_name = "DB_NAME"
db_user = "DB_USER"
db_pass = "DB_PASSWORD"
db_table = "DB_TABLE_NAME"
def create_conn():
conn = None
try:
conn = psycopg2.connect("dbname={} user={} host={} password={}".format(db_name,db_user,db_host,db_pass))
except Exception as e:
print("Cannot connect. {}".format(e))
return conn
def fetch(conn, query):
print("Now executing: {}".format(query))
cursor = conn.cursor()
cursor.execute(query)
raw = cursor.fetchall()
return [line for line in raw]
def main(event, context):
print("HEALTHCHECK!")
# get a connection, if a connect cannot be made an exception will be raised here
conn = create_conn()
query_cmd = "select count(*) from {}".format(db_table)
result = fetch(conn, query_cmd)
conn.close()
return {
"statusCode": 200,
"body": "I'm alive and well! {}".format(result)
}
Seth Geoghegan
10/18/2021, 8:28 PMRoss Coundon
10/18/2021, 9:03 PMSeth Geoghegan
10/18/2021, 9:09 PMWilliam Hatch
10/19/2021, 3:21 AMWilliam Hatch
10/22/2021, 12:12 AMimport { Vpc, SecurityGroup } from '@aws-cdk/aws-ec2';
import StreamStack from './StreamStack.js';
export default function main(app) {
// Set default runtime for all functions
app.setDefaultFunctionProps((stack) => {
let props = {
runtime: 'nodejs12.x',
environment: {
MONGO_URL: process.env.MONGO_URL,
MONGO_DB: process.env.MONGO_DB,
MONGO_COLLECTION: process.env.MONGO_COLLECTION,
},
};
if (app.stage === 'prod') {
//set the vpc attributes in props
const vpc = Vpc.fromLookup(stack, process.env.VPC_ID, {
isDefault: false,
vpcId: process.env.VPC_ID,
});
const securityGroup = SecurityGroup.fromSecurityGroupId(
stack,
'SG',
process.env.SECURITY_GROUP
);
props.vpc = vpc;
props.subnets = vpc.publicSubnets;
props.securityGroups = [securityGroup];
}
return props;
});
new StreamStack(app, 'stream');
}
William Hatch
10/22/2021, 12:15 AMSeth Geoghegan
10/22/2021, 1:09 PMapp.setDefaultFunctionProps
before creating the stack.