-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples/lambda): added new lambda example
- Loading branch information
Showing
13 changed files
with
16,414 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"colour": "green", | ||
"food": "pizza", | ||
"season": "winter" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
}; |
Oops, something went wrong.