Skip to content

Commit

Permalink
docs(examples/lambda): added new lambda example
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed May 1, 2021
1 parent 45b4c91 commit f8ca3c0
Show file tree
Hide file tree
Showing 13 changed files with 16,414 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/lambda/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.js
!jest.config.js
*.d.ts
node_modules

# CDK asset staging directory
.cdk.staging
cdk.out

# project specific
response.json
6 changes: 6 additions & 0 deletions examples/lambda/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.ts
!*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out
40 changes: 40 additions & 0 deletions examples/lambda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Lambda Function

Create a basic Lambda function that can be invoked using the `aws-cli` or AWS Console.

## Getting started

Run `npm install` to get setup.

Have a look at the Lambda code in `./src/index.ts`. Maybe change the returned text to make it more personal. Also have a look at the values in `input.json`, we will use this later.

Once you are happy, deploy your app with `npx cdk deploy`. If you haven't used the CDK in your account before, you will have bootstrap the account first (typically by running `npx cdk bootstrap`) and sort out permissions. Please refer to [the official AWS CDK documentation](https://docs.aws.amazon.com/cdk/latest/guide/home.html) to get started.

As part of the deployment, the ARN of your function will be displayed. We need this for the next step.

To test your function, you could either go into the AWS Console and paste the contents of `input.json` into the test feature.

Alternatively, let's use the aws-cli to invoke our function from the command line:

```bash
aws lambda invoke --function-name "<YOUR LAMBDA ARN HERE>" --payload fileb://input.json response.json
```

You will get a response containing the status of our request. Now check our `response.json` for the results:

```bash
cat response.json
```

Don't forget to tear everything down with `npx cdk destroy` - otherwise you might occur costs.

## Useful commands

- `npm install` start with this
- `npm test` perform the jest unit tests
- `npm start` serve the website on localhost
- `npx cdk bootstrap` setup your AWS environment
- `npx cdk deploy` deploy this stack to your default AWS account/region
- `npx cdk destroy` destroy this stack to ensure you don't occur any costs
- `npx cdk diff` compare deployed stack with current state
- `npx cdk synth` emits the synthesized CloudFormation template
13 changes: 13 additions & 0 deletions examples/lambda/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"app": "npx ts-node --prefer-ts-exts infrastructure/app.ts",
"context": {
"@aws-cdk/core:enableStackNameDuplicates": "true",
"aws-cdk:enableDiffNoFail": "true",
"@aws-cdk/core:stackRelativeExports": "true",
"@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,
"@aws-cdk/aws-secretsmanager:parseOwnedSecretName": true,
"@aws-cdk/aws-kms:defaultKeyPolicies": true,
"@aws-cdk/aws-s3:grantWriteWithoutAcl": true,
"@aws-cdk/aws-ecs-patterns:removeDefaultDesiredCount": true
}
}
6 changes: 6 additions & 0 deletions examples/lambda/infrastructure/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node
import * as cdk from "@aws-cdk/core";
import { LambdaStack } from "./stack";

const app = new cdk.App();
new LambdaStack(app, "Function");
20 changes: 20 additions & 0 deletions examples/lambda/infrastructure/stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as cdk from "@aws-cdk/core";
import { Function, Runtime } from "@aws-cdk/aws-lambda";
import { TypeScriptCode } from "@mrgrain/cdk-esbuild";

export class LambdaStack extends cdk.Stack {
constructor(scope?: cdk.Construct, id?: string, props?: cdk.StackProps) {
super(scope, id, props);


const lambda = new Function(this, "Lambda", {
runtime: Runtime.NODEJS_14_X,
handler: 'index.handler',
code: new TypeScriptCode("./src/index.tsx"),
});

new cdk.CfnOutput(this, "LambdaArn", {
value: lambda.functionArn,
});
}
}
5 changes: 5 additions & 0 deletions examples/lambda/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"colour": "green",
"food": "pizza",
"season": "winter"
}
7 changes: 7 additions & 0 deletions examples/lambda/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
Loading

0 comments on commit f8ca3c0

Please sign in to comment.