Skip to content

Commit ee65d1f

Browse files
committed
Add template project
1 parent 3113a1f commit ee65d1f

23 files changed

+1403
-2
lines changed

.github/workflows/release.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Release
2+
3+
on:
4+
release:
5+
types: [prereleased, edited]
6+
7+
jobs:
8+
build:
9+
name: Build GitHub action and AWS lambda function
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
with:
15+
ref: "main"
16+
17+
- name: Install dependencies
18+
run: yarn install
19+
20+
- name: Get release tag
21+
id: ref
22+
run: |
23+
TAG=${REF#"$PREFIX"}
24+
echo "::set-output name=tag::$TAG"
25+
env:
26+
REF: ${{ github.ref }}
27+
PREFIX: "refs/tags/"
28+
29+
- name: Update package.json version
30+
run: |
31+
tmp=$(mktemp)
32+
jq '.version = "${{ steps.ref.outputs.tag }}"' package.json > "$tmp"
33+
mv "$tmp" package.json
34+
35+
- name: Build lambda distribution
36+
run: yarn dist
37+
38+
- name: Setup GitHub git user
39+
run: |
40+
git config user.name github-actions
41+
git config user.email [email protected]
42+
43+
- name: Push build
44+
run: |
45+
git add package.json ping-slack/dist/index.js terraform/lambda.zip
46+
git commit -m "Build app for release"
47+
git push
48+
49+
- name: Move release tag to latest commit
50+
run: |
51+
git tag --force "${{ steps.ref.outputs.tag }}" $(git rev-parse HEAD)
52+
git push --force --tags

.github/workflows/tests.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
terraform:
11+
name: Validate Terraform code
12+
runs-on: ubuntu-latest
13+
14+
env:
15+
TERRAFORM_VERSION: 0.13.5
16+
TERRAFORM_WORK_DIR: ./terraform
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v2
21+
22+
- name: Setup Terraform
23+
uses: hashicorp/setup-terraform@v1
24+
with:
25+
terraform_version: ${{ env.TERRAFORM_VERSION }}
26+
27+
- name: Terraform Init
28+
working-directory: ${{ env.TERRAFORM_WORK_DIR }}
29+
run: terraform init
30+
31+
- name: Terraform Validate
32+
working-directory: ${{ env.TERRAFORM_WORK_DIR }}
33+
run: terraform validate
34+
env:
35+
AWS_DEFAULT_REGION: ca-central-1 # https://github.com/hashicorp/terraform/issues/21408#issuecomment-495746582
36+
37+
build:
38+
name: Build lambda function
39+
runs-on: ubuntu-latest
40+
41+
steps:
42+
- uses: actions/checkout@v2
43+
44+
- name: Install dependencies
45+
run: yarn install
46+
47+
- name: Build lambda distribution
48+
run: yarn dist
49+
50+
test:
51+
name: Test lambda function
52+
runs-on: ubuntu-latest
53+
54+
steps:
55+
- uses: actions/checkout@v2
56+
57+
- name: Install dependencies
58+
run: yarn install
59+
60+
- name: Test
61+
run: yarn test

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
/dist
3+
.terraform
4+
yarn-error.log

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arrowParens": "avoid",
3+
"printWidth": 120,
4+
"singleQuote": false,
5+
"quoteProps": "consistent"
6+
}

.vscode/extensions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["esbenp.prettier-vscode", "hashicorp.terraform", "annsk.alignment"]
3+
}

.vscode/launch.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Mocha Current File",
11+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
12+
"args": ["--no-timeouts", "--colors", "${file}", "--require", "ts-node/register"],
13+
"console": "integratedTerminal",
14+
"sourceMaps": true,
15+
"internalConsoleOptions": "neverOpen"
16+
},
17+
{
18+
"type": "node",
19+
"request": "launch",
20+
"name": "Debug File",
21+
"program": "${workspaceFolder}/node_modules/.bin/ts-node",
22+
"args": ["${file}"]
23+
}
24+
]
25+
}

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.tabSize": 2,
4+
"editor.trimAutoWhitespace": true
5+
}

CONTRIBUTING.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Contributing
2+
3+
## Publish a new release
4+
5+
- Create a new release (ie.: v1.0.0)
6+
- Check `This is a pre-release`
7+
- A GitHub workflow will run and build the code and update the release.
8+
- Once the build is completed, publish the release.
9+
10+
## Testing
11+
12+
```bash
13+
yarn install
14+
yarn test
15+
```
16+
17+
## Note about `aws-sdk`
18+
19+
`aws-sdk` is placed as a devDependency in the projet in order to build smaller zip file for the distribution.
20+
21+
From the [Building Lambda functions with Node.js](https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html) documentation:
22+
23+
> Your code runs in an environment that includes the AWS SDK for JavaScript, with credentials from an AWS Identity and Access Management (IAM) role that you manage.

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1-
# terraform-aws-lambda
2-
Template repository for creating a TypeScript AWS Lambda with Terraform
1+
# Terraform AWS Lambda Template Repository
2+
3+
_Template repository for creating a TypeScript AWS Lambda with Terraform_
4+
5+
![Release](https://github.com/agendrix/terraform-aws-lambda/workflows/Release/badge.svg) ![Tests](https://github.com/agendrix/terraform-aws-lambda/workflows/Tests/badge.svg?branch=main)
6+
7+
## How to use with Terraform
8+
9+
Add the module to your [Terraform](https://www.terraform.io/) project:
10+
11+
```terraform
12+
module "terraform_aws_lambda" {
13+
source = "[email protected]:agendrix/terraform-aws-lambda.git//terraform?ref=v1.0.0"
14+
lambda_name = "my-typescript-lambda"
15+
role_arn = aws_iam_role.iam_for_lambda.role_arn
16+
}
17+
```
18+
19+
See [Resource: aws_lambda_function](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) for more information about the required `aws_iam_role`.
20+
21+
In order to be able to receive http requests to the lambda, you will need to hook it up with an AWS API Gateway.
22+
You can do so by following this guide: [Serverless Applications with AWS Lambda and API Gateway](https://learn.hashicorp.com/tutorials/terraform/lambda-api-gateway).
23+
24+
---
25+
26+
Your AWS lambda should now be available at https://console.aws.amazon.com/lambda/.
27+
28+
Logs from the lambda will be available in AWS CloudWatch `/aws/lambda/${yourLambdaName}` log group.
29+
30+
## Release a new version of the Terraform module
31+
32+
See the [CONTRIBUTING.md](./CONTRIBUTING.md) file for more info.

lambda/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { APIGatewayEvent, APIGatewayProxyStructuredResultV2, Handler } from "aws-lambda";
2+
import AWS from "aws-sdk";
3+
4+
const handler: Handler<APIGatewayEvent, APIGatewayProxyStructuredResultV2> = async (event, context) => {
5+
const s3 = new AWS.S3();
6+
7+
const data = {
8+
text: "Hello World",
9+
event,
10+
context,
11+
listBuckets: await s3.listBuckets().promise(),
12+
};
13+
14+
return {
15+
statusCode: 200,
16+
body: JSON.stringify(data),
17+
headers: { "Content-Type": "text/html; charset=utf-8" },
18+
isBase64Encoded: false,
19+
};
20+
};
21+
22+
exports.handler = handler;
23+
24+
export const __test__ = {
25+
handler,
26+
};

0 commit comments

Comments
 (0)