Carlos Daniel
01/20/2022, 6:55 PMMichael Wolfenden
01/20/2022, 11:47 PMMichael Wolfenden
01/20/2022, 11:50 PMAdrián Mouly
01/21/2022, 1:10 AMAdrián Mouly
01/21/2022, 1:10 AMBjorn Theart
01/21/2022, 6:57 AMimport * as cdk from 'aws-cdk-lib';
import config from '../src/config';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as sst from '@serverless-stack/resources';
import * as apigatewayv2 from '@aws-cdk/aws-apigatewayv2-alpha';
class Serverless extends sst.Stack {
constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
super(scope, id, props);
const { stage, region, name } = scope;
const { account } = this;
const env = {
STAGE: stage,
REGION: region,
ACCOUNT_ID: account,
};
// Setup Lambda handlers
const lambda = {
createDeal: new sst.Function(this, 'createDeal', {
handler: 'src/deal/create.handler',
environment: env,
}),
updateDeal: new sst.Function(this, 'updateDeal', {
handler: 'src/deal/update.handler',
environment: env,
}),
createContact: new sst.Function(this, 'createContact', {
handler: 'src/contact/create.handler',
environment: env,
}),
};
/*
* Instantiate a new IAM policy statement that allows reading of
* a parameter that stores our Hubspot API key.
*/
const ssmParameterPolicy = new iam.PolicyStatement({
actions: [
'ssm:GetParameter',
'ssm:GetParameters',
'ssm:GetParametersByPath',
],
effect: iam.Effect.ALLOW,
resources: [
`arn:aws:ssm:${region}:${account}:parameter/${config.hubspotApiKey}`,
],
});
// Attach the policy statement to the Lambda functions
lambda.createDeal.attachPermissions([ssmParameterPolicy]);
lambda.updateDeal.attachPermissions([ssmParameterPolicy]);
lambda.createContact.attachPermissions([ssmParameterPolicy]);
// Create an HTTP API
const api = new sst.Api(this, 'Api', {
cors: {
allowHeaders: ['Content-Type', 'Authorization'],
allowMethods: [
apigatewayv2.CorsHttpMethod.GET,
<http://apigatewayv2.CorsHttpMethod.POST|apigatewayv2.CorsHttpMethod.POST>,
],
allowOrigins: ['*'],
maxAge: cdk.Duration.days(10),
},
routes: {
'POST /deals': { function: lambda.createDeal },
'PUT /deals/{dealId}': { function: lambda.updateDeal },
'POST /contacts': { function: lambda.createContact },
},
accessLog: true,
});
// Show the endpoint in the output
this.addOutputs({
ApiEndpoint: api.url,
});
}
}
// serverless app
export default function main(app: <http://sst.App|sst.App>) {
// extract the meta from the app
const { stage, region, name } = app;
// instantiate a serverless stack
new Serverless(app, 'serverless', {
description: `Serverless stack for app: ${name} stage:${stage} in the ${region} region`,
});
}
Carlos Daniel
01/21/2022, 4:01 PMCarlos Daniel
01/21/2022, 4:02 PMAdrián Mouly
01/21/2022, 4:49 PMAdrián Mouly
01/21/2022, 4:49 PMCarlos Daniel
01/21/2022, 4:50 PM