Skip to content

Commit fc0384e

Browse files
authored
Merge pull request #29 from gunnargrosch/develop
Develop
2 parents 3d2d90e + 31725de commit fc0384e

24 files changed

+18436
-59
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
.aws-sam/
12
.vs/
23
.vscode/
34
.serverless/
45
node_modules/
56
example/.vscode/
67
example/.serverless/
78
example/node_modules/
9+
samconfig.toml

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,27 @@ Edit the values of your parameter in SSM Parameter Store or hosted configuration
7575

7676
## Example
7777

78-
In the subfolder `example` is a simple Serverless Framework template which will install a Lambda function and a parameter in SSM Parameter Store.
78+
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.
79+
80+
### AWS SAM
81+
```bash
82+
cd example/sam
83+
npm install
84+
sam build
85+
sam deploy --guided
86+
```
87+
88+
### AWS CDK
89+
```bash
90+
cd example/cdk
91+
npm install
92+
cdk deploy
93+
```
94+
95+
### Serverless Framework
7996
```bash
80-
npm i failure-lambda
97+
cd example/sls
98+
npm install
8199
sls deploy
82100
```
83101

example/.eslintrc.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

example/cdk/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
!lib/resources/index.js
6+
7+
# CDK asset staging directory
8+
.cdk.staging
9+
cdk.out

example/cdk/.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
import 'source-map-support/register';
3+
import * as cdk from 'aws-cdk-lib';
4+
import { FailureLambdaExampleStack } from '../lib/failure_lambda_example-stack';
5+
6+
const app = new cdk.App();
7+
new FailureLambdaExampleStack(app, 'FailureLambdaExampleStack', {
8+
/* If you don't specify 'env', this stack will be environment-agnostic.
9+
* Account/Region-dependent features and context lookups will not work,
10+
* but a single synthesized template can be deployed anywhere. */
11+
12+
/* Uncomment the next line to specialize this stack for the AWS Account
13+
* and Region that are implied by the current CLI configuration. */
14+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
15+
16+
/* Uncomment the next line if you know exactly what Account and Region you
17+
* want to deploy the stack to. */
18+
// env: { account: '123456789012', region: 'us-east-1' },
19+
20+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
21+
});

example/cdk/cdk.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/failure_lambda_example.ts",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"tsconfig.json",
13+
"package*.json",
14+
"yarn.lock",
15+
"node_modules",
16+
"test"
17+
]
18+
},
19+
"context": {
20+
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
21+
"@aws-cdk/core:stackRelativeExports": true,
22+
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
23+
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
24+
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
25+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
26+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
27+
"@aws-cdk/core:target-partitions": [
28+
"aws",
29+
"aws-cn"
30+
]
31+
}
32+
}

example/cdk/jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/test'],
4+
testMatch: ['**/*.test.ts'],
5+
transform: {
6+
'^.+\\.tsx?$': 'ts-jest'
7+
}
8+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Stack, StackProps } from "aws-cdk-lib";
2+
import { Construct } from "constructs";
3+
import * as cdk from "aws-cdk-lib";
4+
import * as lambda from "aws-cdk-lib/aws-lambda";
5+
import * as lambdaNode from "aws-cdk-lib/aws-lambda-nodejs";
6+
import * as apigateway from "aws-cdk-lib/aws-apigateway";
7+
import * as ssm from "aws-cdk-lib/aws-ssm";
8+
import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
9+
10+
export class FailureLambdaExampleStack extends Stack {
11+
constructor(scope: Construct, id: string, props?: StackProps) {
12+
super(scope, id, props);
13+
14+
const failureLambdaParameter = new ssm.StringParameter(this, "failureLambdaParameter", {
15+
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"]}',
16+
}
17+
);
18+
const failureLambdaTable = new dynamodb.Table(this, "failureLambdaTable", {
19+
removalPolicy: cdk.RemovalPolicy.DESTROY,
20+
partitionKey: { name: "id", type: dynamodb.AttributeType.NUMBER },
21+
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
22+
});
23+
24+
const failureLambdaFunction = new lambdaNode.NodejsFunction(this, "failureLambdaFunction", {
25+
runtime: lambda.Runtime.NODEJS_14_X,
26+
handler: "handler",
27+
entry: `${__dirname}/resources/index.js`,
28+
environment: {
29+
FAILURE_INJECTION_PARAM: failureLambdaParameter.parameterName,
30+
FAILURE_INJECTION_TABLE: failureLambdaTable.tableName,
31+
},
32+
bundling: {
33+
nodeModules: ["failure-lambda"],
34+
},
35+
}
36+
);
37+
38+
new apigateway.LambdaRestApi(this, "failureLambdaApi", {
39+
handler: failureLambdaFunction,
40+
});
41+
42+
failureLambdaParameter.grantRead(failureLambdaFunction);
43+
failureLambdaTable.grantWriteData(failureLambdaFunction);
44+
45+
}
46+
}

example/index.js renamed to example/cdk/lib/resources/index.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
'use strict'
22
const failureLambda = require('failure-lambda')
3-
const fs = require('fs')
43
const AWS = require('aws-sdk')
5-
const s3 = new AWS.S3()
64
const dynamoDb = new AWS.DynamoDB.DocumentClient()
75
let response
86

97
exports.handler = failureLambda(async (event, context) => {
108
try {
11-
let fileName = Date.now() + '.tmp'
12-
let contents = 'Hello failureLambda!'
13-
fs.writeFile('/tmp/' + fileName, contents, (err) => {
14-
if (err) throw err
15-
})
16-
let s3Params = {
17-
Bucket: process.env.FAILURE_INJECTION_BUCKET,
18-
Key: fileName,
19-
Body: contents
20-
}
21-
s3.upload(s3Params, (err) => {
22-
if (err) throw err
23-
})
24-
let ddbParams = {
9+
const contents = 'Hello failureLambda!'
10+
const ddbParams = {
2511
TableName: process.env.FAILURE_INJECTION_TABLE,
2612
Item: {
2713
id: Date.now(),

example/cdk/lib/resources/package-lock.json

Lines changed: 144 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "failure-lambda-example",
3+
"version": "0.1.0",
4+
"description": "",
5+
"dependencies": {
6+
"failure-lambda": "0.4.2"
7+
}
8+
}

0 commit comments

Comments
 (0)