diff --git a/aws-ts-lambda-efs/index.ts b/aws-ts-lambda-efs/index.ts index a4b61a865..2007175a5 100644 --- a/aws-ts-lambda-efs/index.ts +++ b/aws-ts-lambda-efs/index.ts @@ -1,26 +1,37 @@ // 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 = 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 }, @@ -28,7 +39,7 @@ export = async () => { }, { dependsOn: targets }); // Lambda - function efsvpcCallback(name: string, f: aws.lambda.Callback) { + function efsvpcCallback(name: string, f: aws.lambda.Callback) { return new aws.lambda.CallbackFunction(name, { policies: [aws.iam.ManagedPolicy.AWSLambdaVPCAccessExecutionRole, aws.iam.ManagedPolicy.LambdaFullAccess], vpcConfig: { @@ -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) => { @@ -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, @@ -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, @@ -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 diff --git a/aws-ts-lambda-efs/package.json b/aws-ts-lambda-efs/package.json index 04574108e..fa9dd27df 100644 --- a/aws-ts-lambda-efs/package.json +++ b/aws-ts-lambda-efs/package.json @@ -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" } }