Skip to content

Commit

Permalink
Updated to use new versions of packages (pulumi#1565)
Browse files Browse the repository at this point in the history
- v6 of AWS provider
- v2 of AWSx
- v2 of AWS API Gateway MLC
  • Loading branch information
pierskarsenbarg committed Mar 2, 2024
1 parent 8d4d203 commit 00393ba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
68 changes: 43 additions & 25 deletions aws-ts-lambda-efs/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.

import * as aws from "@pulumi/aws";
import * as apigateway from "@pulumi/aws-apigateway";
import * as awsx from "@pulumi/awsx";
import * as pulumi from "@pulumi/pulumi";
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
import * as cp from "child_process";
import * as fs from "fs";

export = async () => {

// VPC
const vpc = new awsx.ec2.Vpc("vpc", { subnets: [{ type: "private" }, { type: "public" }] });
const vpc = new awsx.ec2.Vpc("vpc", {
subnetStrategy: "Auto",
enableDnsHostnames: true,
});
const subnetIds = await vpc.publicSubnetIds;

// EFS
const filesystem = new aws.efs.FileSystem("filesystem");
const targets = [];
for (let i = 0; i < subnetIds.length; i++) {
targets.push(new aws.efs.MountTarget(`fs-mount-${i}`, {
fileSystemId: filesystem.id,
subnetId: subnetIds[i],
securityGroups: [vpc.vpc.defaultSecurityGroupId],
}));
}

const targets: pulumi.Output<aws.efs.MountTarget[]> = subnetIds.apply(ids => {
const targetArray: aws.efs.MountTarget[] = [];
for (let i = 0; i < ids.length; i++) {
targetArray.push(new aws.efs.MountTarget(`fs-mount-${i}`, {
fileSystemId: filesystem.id,
subnetId: subnetIds[i],
securityGroups: [vpc.vpc.defaultSecurityGroupId],
}));
}
return targetArray;
});

const ap = new aws.efs.AccessPoint("ap", {
fileSystemId: filesystem.id,
posixUser: { uid: 1000, gid: 1000 },
rootDirectory: { path: "/www", creationInfo: { ownerGid: 1000, ownerUid: 1000, permissions: "755" } },
}, { dependsOn: targets });

// Lambda
function efsvpcCallback(name: string, f: aws.lambda.Callback<awsx.apigateway.Request, awsx.apigateway.Response>) {
function efsvpcCallback(name: string, f: aws.lambda.Callback<APIGatewayProxyEvent, APIGatewayProxyResult>) {
return new aws.lambda.CallbackFunction(name, {
policies: [aws.iam.ManagedPolicy.AWSLambdaVPCAccessExecutionRole, aws.iam.ManagedPolicy.LambdaFullAccess],
vpcConfig: {
Expand All @@ -37,11 +48,11 @@ export = async () => {
},
fileSystemConfig: { arn: ap.arn, localMountPath: "/mnt/storage" },
callback: f,
});
}, {dependsOn: targets});
}

// API Gateway
const api = new awsx.apigateway.API("api", {
const api = new apigateway.RestAPI("api", {
routes: [
{
method: "GET", path: "/files/{filename+}", eventHandler: efsvpcCallback("getHandler", async (ev, ctx) => {
Expand All @@ -61,7 +72,7 @@ export = async () => {
method: "POST", path: "/files/{filename+}", eventHandler: efsvpcCallback("uploadHandler", async (ev, ctx) => {
try {
const f = "/mnt/storage/" + ev.pathParameters!.filename;
const data = new Buffer(ev.body!, "base64");
const data = Buffer.from(ev.body!, "base64");
fs.writeFileSync(f, data);
return {
statusCode: 200,
Expand All @@ -74,7 +85,7 @@ export = async () => {
},
{
method: "POST", path: "/", eventHandler: efsvpcCallback("execHandler", async (ev, ctx) => {
const cmd = new Buffer(ev.body!, "base64").toString();
const cmd = Buffer.from(ev.body!, "base64").toString();
const buf = cp.execSync(cmd);
return {
statusCode: 200,
Expand All @@ -86,29 +97,36 @@ export = async () => {
});

// ECS Cluster
const cluster = new awsx.ecs.Cluster("cluster", { vpc: vpc });
const efsVolumeConfiguration: aws.types.input.ecs.TaskDefinitionVolumeEfsVolumeConfiguration = {
fileSystemId: filesystem.id,
authorizationConfig: { accessPointId: ap.id },
rootDirectory: "/www",
transitEncryption: "ENABLED",
const cluster = new aws.ecs.Cluster("cluster");

const efsVolume: aws.types.input.ecs.TaskDefinitionVolume = {
name: "efs",
efsVolumeConfiguration: {
fileSystemId: filesystem.id,
authorizationConfig: { accessPointId: ap.id },
transitEncryption: "ENABLED",
},
};

// Fargate Service
const nginx = new awsx.ecs.FargateService("nginx", {
cluster: cluster,
cluster: cluster.arn,
taskDefinitionArgs: {
container: {
image: "nginx",
name: "niginx",
memory: 128,
portMappings: [{ containerPort: 80, hostPort: 80, protocol: "tcp" }],
mountPoints: [{ containerPath: "/usr/share/nginx/html", sourceVolume: "efs" }],
mountPoints: [{ containerPath: "/usr/share/nginx/html", sourceVolume: efsVolume.name }],
},
volumes: [{ name: "efs", efsVolumeConfiguration }],
volumes: [efsVolume],
},
securityGroups: [vpc.vpc.defaultSecurityGroupId, ...cluster.securityGroups],
subnets: vpc.publicSubnetIds,
platformVersion: "1.4.0",
networkConfiguration: {
securityGroups: [vpc.vpc.defaultSecurityGroupId],
subnets: vpc.publicSubnetIds,
assignPublicIp: true,
},
});

// Exports
Expand Down
8 changes: 5 additions & 3 deletions aws-ts-lambda-efs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"name": "aws-ts-lambda-efs",
"version": "0.1.0",
"dependencies": {
"@pulumi/aws": "^5.0.0",
"@pulumi/awsx": "^0.40.0",
"@pulumi/pulumi": "^3.0.0"
"@pulumi/aws": "^6.0.0",
"@pulumi/aws-apigateway": "^2.1.1",
"@pulumi/awsx": "^2.0.0",
"@pulumi/pulumi": "^3.0.0",
"aws-lambda": "^1.0.7"
}
}

0 comments on commit 00393ba

Please sign in to comment.