Skip to content

Instantly share code, notes, and snippets.

@rhyek
Last active March 28, 2022 00:09
Show Gist options
  • Save rhyek/89357f42e93c72c08dd615f5382bd155 to your computer and use it in GitHub Desktop.
Save rhyek/89357f42e93c72c08dd615f5382bd155 to your computer and use it in GitHub Desktop.
Pulumi, AWS API Gateway (HTTP API V2), AWS Lambda, Node.js
import * as pulumi from '@pulumi/pulumi';
import * as aws from '@pulumi/aws';
const functionName = 'createOrder';
const compiledAppPath = '../src/backend/create-order-lambda/dist';
const projectTag = { Project: 'some-project-name-1' };
const lambdaRole = new aws.iam.Role(`${functionName}LambdaRole`, {
assumeRolePolicy: {
Version: '2012-10-17',
Statement: [
{
Action: 'sts:AssumeRole',
Principal: {
Service: 'lambda.amazonaws.com',
},
Effect: 'Allow',
Sid: '',
},
],
},
tags: projectTag,
});
new aws.iam.RolePolicyAttachment(`${functionName}RolePolicyAttachment`, {
role: lambdaRole,
policyArn: aws.iam.ManagedPolicy.AWSLambdaExecute,
});
const lambdaFunction = new aws.lambda.Function(`${functionName}Lambda`, {
architectures: ['arm64'],
runtime: 'nodejs14.x',
role: lambdaRole.arn,
handler: 'main.handler',
code: new pulumi.asset.AssetArchive({
'.': new pulumi.asset.FileArchive(compiledAppPath),
}),
tags: projectTag,
});
const apiGateway = new aws.apigatewayv2.Api(`${functionName}ApiGateway`, {
protocolType: 'HTTP',
target: lambdaFunction.arn,
tags: projectTag,
});
new aws.lambda.Permission(`${functionName}ApiGatewayInvokeLambdaPermission`, {
action: 'lambda:InvokeFunction',
function: lambdaFunction.arn,
principal: 'apigateway.amazonaws.com',
sourceArn: apiGateway.executionArn.apply((v) => `${v}/*/*`),
});
export const apiGatewayUrl = apiGateway.apiEndpoint;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment