Skip to content

Commit

Permalink
Added new sample for SAM, CDK, and SLS
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnargrosch committed Feb 14, 2022
1 parent 3d2d90e commit d80134b
Show file tree
Hide file tree
Showing 24 changed files with 18,436 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
.aws-sam/
.vs/
.vscode/
.serverless/
node_modules/
example/.vscode/
example/.serverless/
example/node_modules/
samconfig.toml
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,27 @@ Edit the values of your parameter in SSM Parameter Store or hosted configuration

## Example

In the subfolder `example` is a simple Serverless Framework template which will install a Lambda function and a parameter in SSM Parameter Store.
In the subfolder `example` is a sample application which will install an AWS Lambda function, an Amazon DynamoDB table, and a parameter in SSM Parameter Store. You can install it using AWS SAM, AWS CDK, or Serverless Framework.

### AWS SAM
```bash
cd example/sam
npm install
sam build
sam deploy --guided
```

### AWS CDK
```bash
cd example/cdk
npm install
cdk deploy
```

### Serverless Framework
```bash
npm i failure-lambda
cd example/sls
npm install
sls deploy
```

Expand Down
1 change: 0 additions & 1 deletion example/.eslintrc.js

This file was deleted.

9 changes: 9 additions & 0 deletions example/cdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.js
!jest.config.js
*.d.ts
node_modules
!lib/resources/index.js

# CDK asset staging directory
.cdk.staging
cdk.out
6 changes: 6 additions & 0 deletions example/cdk/.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
21 changes: 21 additions & 0 deletions example/cdk/bin/failure_lambda_example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import { FailureLambdaExampleStack } from '../lib/failure_lambda_example-stack';

const app = new cdk.App();
new FailureLambdaExampleStack(app, 'FailureLambdaExampleStack', {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */

/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },

/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
32 changes: 32 additions & 0 deletions example/cdk/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"app": "npx ts-node --prefer-ts-exts bin/failure_lambda_example.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
},
"context": {
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
"@aws-cdk/core:stackRelativeExports": true,
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
"@aws-cdk/core:target-partitions": [
"aws",
"aws-cn"
]
}
}
8 changes: 8 additions & 0 deletions example/cdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
46 changes: 46 additions & 0 deletions example/cdk/lib/failure_lambda_example-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as cdk from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as lambdaNode from "aws-cdk-lib/aws-lambda-nodejs";
import * as apigateway from "aws-cdk-lib/aws-apigateway";
import * as ssm from "aws-cdk-lib/aws-ssm";
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";

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

const failureLambdaParameter = new ssm.StringParameter(this, "failureLambdaParameter", {
stringValue: '{"isEnabled": false, "failureMode": "latency", "rate": 1, "minLatency": 100, "maxLatency": 400, "exceptionMsg": "Exception message!", "statusCode": 404, "diskSpace": 100, "denylist": ["s3.*.amazonaws.com", "dynamodb.*.amazonaws.com"]}',
}
);
const failureLambdaTable = new dynamodb.Table(this, "failureLambdaTable", {
removalPolicy: cdk.RemovalPolicy.DESTROY,
partitionKey: { name: "id", type: dynamodb.AttributeType.NUMBER },
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
});

const failureLambdaFunction = new lambdaNode.NodejsFunction(this, "failureLambdaFunction", {
runtime: lambda.Runtime.NODEJS_14_X,
handler: "handler",
entry: `${__dirname}/resources/index.js`,
environment: {
FAILURE_INJECTION_PARAM: failureLambdaParameter.parameterName,
FAILURE_INJECTION_TABLE: failureLambdaTable.tableName,
},
bundling: {
nodeModules: ["failure-lambda"],
},
}
);

new apigateway.LambdaRestApi(this, "failureLambdaApi", {
handler: failureLambdaFunction,
});

failureLambdaParameter.grantRead(failureLambdaFunction);
failureLambdaTable.grantWriteData(failureLambdaFunction);

}
}
18 changes: 2 additions & 16 deletions example/index.js → example/cdk/lib/resources/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
'use strict'
const failureLambda = require('failure-lambda')
const fs = require('fs')
const AWS = require('aws-sdk')
const s3 = new AWS.S3()
const dynamoDb = new AWS.DynamoDB.DocumentClient()
let response

exports.handler = failureLambda(async (event, context) => {
try {
let fileName = Date.now() + '.tmp'
let contents = 'Hello failureLambda!'
fs.writeFile('/tmp/' + fileName, contents, (err) => {
if (err) throw err
})
let s3Params = {
Bucket: process.env.FAILURE_INJECTION_BUCKET,
Key: fileName,
Body: contents
}
s3.upload(s3Params, (err) => {
if (err) throw err
})
let ddbParams = {
const contents = 'Hello failureLambda!'
const ddbParams = {
TableName: process.env.FAILURE_INJECTION_TABLE,
Item: {
id: Date.now(),
Expand Down
144 changes: 144 additions & 0 deletions example/cdk/lib/resources/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions example/cdk/lib/resources/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "failure-lambda-example",
"version": "0.1.0",
"description": "",
"dependencies": {
"failure-lambda": "0.4.2"
}
}
Loading

0 comments on commit d80134b

Please sign in to comment.