Skip to content

Instantly share code, notes, and snippets.

@iDVB
Last active September 23, 2021 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iDVB/641399bf28708fc33a317f73f04ebe5a to your computer and use it in GitHub Desktop.
Save iDVB/641399bf28708fc33a317f73f04ebe5a to your computer and use it in GitHub Desktop.
import * as sst from '@serverless-stack/resources'
import * as iam from '@aws-cdk/aws-iam'
import * as apigw from '@aws-cdk/aws-apigatewayv2'
export default class MyStack extends sst.Stack {
constructor(scope: sst.App, id: string, props?: sst.StackProps) {
super(scope, id, props)
const { account, region } = scope
// Create a HTTP API
const api = new sst.Api(this, 'Api', {
defaultFunctionProps: {
timeout: 20,
},
routes: {
'GET /callback': 'src/lambda.callback',
'GET /install': 'src/lambda.install',
},
})
const serviceRole = new iam.Role(this, 'service-role', {
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
inlinePolicies: {
AllowAPIGServiceToAssumeRole: new iam.PolicyDocument({
statements: [
new iam.PolicyStatement({
actions: ['events:PutEvents'],
effect: iam.Effect.ALLOW,
resources: [`arn:aws:events:${region}:${account}:event-bus/default`],
}),
],
}),
},
})
const integ = new apigw.CfnIntegration(this, 'Integ', {
apiId: api.httpApi.httpApiId,
integrationType: 'AWS_PROXY',
integrationSubtype: 'EventBridge-PutEvents',
credentialsArn: serviceRole.roleArn,
requestParameters: {
Source: 'Gateway',
Detail: '$request.body',
DetailType: 'api-call',
// EventBusName: eventBus.eventBusArn,
},
payloadFormatVersion: '1.0',
timeoutInMillis: 10000,
})
new apigw.CfnRoute(this, 'DefaultRoute', {
apiId: api.httpApi.httpApiId,
routeKey: 'ANY /webhook',
target: `integrations/${integ.ref}`,
})
// Show the endpoint in the output
this.addOutputs({
ApiEndpoint: api.url,
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment