Skip to content

Instantly share code, notes, and snippets.

@neilkuan
Created June 6, 2021 10:43
Show Gist options
  • Save neilkuan/0e3e118fd0fc537e037d2fe4b08e2ea1 to your computer and use it in GitHub Desktop.
Save neilkuan/0e3e118fd0fc537e037d2fe4b08e2ea1 to your computer and use it in GitHub Desktop.
s3-lambda-cdk-sample.ts
import * as lambda from '@aws-cdk/aws-lambda';
import * as s3 from '@aws-cdk/aws-s3';
import { App, Construct, Stack, StackProps, CfnOutput, RemovalPolicy } from '@aws-cdk/core';
// get date function.
function getdate() {
var date = new Date();
var mm = date.getMonth() + 1; // getMonth() is zero-based
var dd = date.getDate();
return [date.getFullYear(),
(mm>9 ? '' : '0') + mm,
(dd>9 ? '' : '0') + dd].join('');
};
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps = {}) {
super(scope, id, props);
const mybucket = new s3.Bucket(this, 'mybucket', {
bucketName: `mybucket-create-by-aws-cdk-${getdate()}`,
removalPolicy: RemovalPolicy.DESTROY,
});
const lambda_s3 = new lambda.Function(this, 'initFun', {
code: lambda.InlineCode.fromInline(`
import os
bucket = os.getenv('S3_BUCKET')
def handler(event, context):
return f'Hello initline Python Lambda, bucket name is {bucket}'`),
runtime: lambda.Runtime.PYTHON_3_8,
handler: 'index.handler',
environment: { S3_BUCKET: mybucket.bucketName },
});
new CfnOutput(this, 'mybucket_output', {
value: mybucket.bucketName,
});
new CfnOutput(this, 'lambda_s3_output', {
value: lambda_s3.functionName,
});
}
}
const devEnv = {
account: process.env.CDK_DEFAULT_ACCOUNT,
region: process.env.CDK_DEFAULT_REGION,
};
const app = new App();
new MyStack(app, 'sys-ops-demo', { env: devEnv });
app.synth();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment