Skip to content

Commit

Permalink
adding scheduled functions example
Browse files Browse the repository at this point in the history
  • Loading branch information
aydrian committed Jul 23, 2019
1 parent c9c06e8 commit 4fecf26
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aws-ts-scheduled-function/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/
/node_modules/
3 changes: 3 additions & 0 deletions aws-ts-scheduled-function/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: aws-ts-scheduled-function
runtime: nodejs
description: A minimal AWS TypeScript Pulumi program
37 changes: 37 additions & 0 deletions aws-ts-scheduled-function/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as aws from "@pulumi/aws";

// Create an AWS resource (S3 Bucket)
const trashBucket = new aws.s3.Bucket("trash");

// A handler function that will list objects in the bucket and bulk delete them
const emptyTrash: aws.cloudwatch.EventRuleEventHandler = async (
event: aws.cloudwatch.EventRuleEvent
) => {
const s3Client = new aws.sdk.S3();
const bucket = trashBucket.id.get();

const { Contents = [] } = await s3Client
.listObjects({ Bucket: bucket })
.promise();
const objects = Contents.map(object => {
return { Key: object.Key };
});

await s3Client
.deleteObjects({
Bucket: bucket,
Delete: { Objects: objects, Quiet: false }
})
.promise();
};

// Schedule the function to run every Friday at 11:00pm UTC (6:00pm EST)
// More info on Schedule Expressions at https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html
const emptyTrashSchedule: aws.cloudwatch.EventRuleEventSubscription = aws.cloudwatch.onSchedule(
"emptyTrash",
"cron(0 23 ? * FRI *)",
emptyTrash
);

// Export the name of the bucket
export const bucketName = trashBucket.id;
11 changes: 11 additions & 0 deletions aws-ts-scheduled-function/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "aws-typescript",
"devDependencies": {
"@types/node": "latest"
},
"dependencies": {
"@pulumi/pulumi": "latest",
"@pulumi/aws": "latest",
"@pulumi/awsx": "latest"
}
}
22 changes: 22 additions & 0 deletions aws-ts-scheduled-function/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es6",
"lib": [
"es6"
],
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true
},
"files": [
"index.ts"
]
}

0 comments on commit 4fecf26

Please sign in to comment.