-
Notifications
You must be signed in to change notification settings - Fork 8
/
s3-upload-file-stack.ts
43 lines (38 loc) · 1.42 KB
/
s3-upload-file-stack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { aws_apigateway, aws_lambda, aws_lambda_nodejs, aws_s3, Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as path from 'path';
export class S3UploadFileStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const bucket = new aws_s3.Bucket(this, 'Bucket', {});
const signedUrlLambda = new aws_lambda_nodejs.NodejsFunction(this, 'SignedUrlLambda', {
runtime: aws_lambda.Runtime.NODEJS_14_X,
handler: 'handler',
entry: path.join(__dirname, '..', 'lambdas', 'signed-url', 'index.ts'),
timeout: Duration.seconds(10),
environment: {
BUCKET: bucket.bucketName,
},
bundling: {
sourceMap: false,
minify: false,
target: 'es2020',
externalModules: ['aws-sdk'],
},
});
bucket.grantWrite(signedUrlLambda);
// Proxy API Gateway
const api = new aws_apigateway.LambdaRestApi(this, 'ProxyApiGateway', {
handler: signedUrlLambda,
restApiName: 'example-s3-upload',
apiKeySourceType: aws_apigateway.ApiKeySourceType.HEADER,
defaultMethodOptions: {
apiKeyRequired: true,
},
});
const apiKey = api.addApiKey('example-signed-url-api-key');
const usagePlan = api.addUsagePlan('example-s3-upload');
usagePlan.addApiKey(apiKey);
usagePlan.addApiStage({ stage: api.deploymentStage });
}
}