Skip to content

Commit 974215b

Browse files
Refactor lambda source code (#11)
* (remove): upload code from s3 function * (fix): ref to non-declare resource * (add): new idea to manage terraform * (update): source code to new algo * (fix): lambda edge variable overflow * (update): document * (update): README.md * (remove): comment path * (update): change additional policies to map instead of list deu to un-predicted variable * (update): s3 version to v1.0.4 * (add): local tag to ssm parameter store * (add): tracing mode enable * (add): change log * (fix): set default tracing mode if received from upstream * (update): order of variables * (update): CHANGELOG * (update): README * chore: add community friendly templates * (update): CHANGELOG * (update): .gitignore * (update): variable name and README * (update): variable name and README * (update): README * (update): variables naming * (add): complete example * (update): repo name * (update): example for simple usage * (update): example simple * (update): example simple * (update): lambda complete usage * (update): lambda complete usage * (update): README and CHANGELOG * (update): README and CHANGELOG * (update): exmaple format and versioning format * (update): README.md for example Co-authored-by: Pongsak Sanguanwong <pongsak.sanguanwong@gmail.com>
1 parent 85b21e6 commit 974215b

21 files changed

+451
-166
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ crash.*.log
2424
# to change depending on the environment.
2525
#
2626
*.tfvars
27+
!terraform.*example*.tfvars
2728

2829
# Ignore override files as they are usually used to override resources locally and so
2930
# are not checked in

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
args:
1212
- "--args=--only=terraform_deprecated_interpolation"
1313
- "--args=--only=terraform_deprecated_index"
14-
# - "--args=--only=terraform_unused_declarations"
14+
- "--args=--only=terraform_unused_declarations"
1515
- "--args=--only=terraform_comment_syntax"
1616
- "--args=--only=terraform_documented_outputs"
1717
- "--args=--only=terraform_documented_variables"

CHANGELOG.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Change Log
2+
3+
All notable changes to this module will be documented in this file.
4+
5+
## [1.1.0] - 2022-07-22
6+
7+
### Changed
8+
9+
- Remove upload code from s3
10+
- S3 source code is used for versioning
11+
- Change `additional_lambda_role_policy_arn` to map from list
12+
13+
### Added
14+
15+
- Enable Tracing
16+
17+
## [v1.0.2] - 2022-07-01
18+
19+
### Added
20+
21+
- Add default log retention 90 days, KMS encryption support
22+
23+
### Fixed
24+
25+
- Fix kms security issue by @xshot9011 in #9
26+
27+
## [v1.0.1] - 2022-06-08
28+
29+
### Added
30+
31+
- Add resource base policy for lambda
32+
33+
## [v1.0.0] - 2022-05-17
34+
35+
### Added
36+
37+
- Since Lambdas are uploaded via zip files, we generate a zip file from the path specified.
38+
- Upload the zip file containing the build artifacts to S3.
39+
- Allow access to this lambda function from AWS.
40+
- Allow lambda to generate logs.
41+
- Construct a role that AWS services can adopt in order to invoke our function.
42+
- This policy also has the capability to write logs to CloudWatch.
43+
- Create the secret SSM parameters that can be retrieved and decoded by the lambda function.
44+
- Create an IAM policy document granting the ability to read and retrieve SSM parameter values.
45+
- Develop a policy based on the SSM policy paper
46+
- Custom policies to attach to this role

README.md

Lines changed: 39 additions & 101 deletions
Large diffs are not rendered by default.

examples/complete/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Requirements
3+
4+
| Name | Version |
5+
|---------------------------------------------------------------------------|----------|
6+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
7+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0 |
8+
9+
## Providers
10+
11+
No providers.
12+
13+
## Modules
14+
15+
| Name | Source | Version |
16+
|--------------------------------------------------------|--------|---------|
17+
| <a name="module_lambda"></a> [lambda](#module\_lambda) | ../../ | n/a |
18+
19+
## Resources
20+
21+
No resources.
22+
23+
## Inputs
24+
25+
| Name | Description | Type | Default | Required |
26+
|-----------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|------------|---------|:--------:|
27+
| <a name="input_custom_tags"></a> [custom\_tags](#input\_custom\_tags) | Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys | `map(any)` | `{}` | no |
28+
| <a name="input_environment"></a> [environment](#input\_environment) | Environment Variable used as a prefix | `string` | n/a | yes |
29+
| <a name="input_name"></a> [name](#input\_name) | Name of the ECS cluster and s3 also redis to create | `string` | n/a | yes |
30+
| <a name="input_prefix"></a> [prefix](#input\_prefix) | The prefix name of customer to be displayed in AWS console and resource | `string` | n/a | yes |
31+
32+
## Outputs
33+
34+
No outputs.
35+
<!-- END_TF_DOCS -->

examples/complete/main.tf

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
module "lambda" {
2+
source = "../../"
3+
4+
prefix = var.prefix
5+
environment = var.environment
6+
name = var.name
7+
8+
is_edge = false # Defautl is `false`, If you want to publish to the edge don't forget to override aws's provider to virgina
9+
10+
# If is_edge is `false`, ignore this config
11+
is_create_lambda_bucket = true # Default is `false`; plz use false, if not 1 lambda: 1 bucket
12+
bucket_name = "" # If `is_create_lambda_bucket` is `false`; specified this, default is `""`
13+
14+
# Source code
15+
source_code_dir = "./src"
16+
file_globs = ["index.js"]
17+
compressed_local_file_dir = "./outputs"
18+
19+
# Lambda Env
20+
runtime = "nodejs12.x"
21+
handler = "index.handler"
22+
23+
# Lambda Specification
24+
timeout = 3
25+
memory_size = 128
26+
reserved_concurrent_executions = -1
27+
28+
# Optional to connect Lambda to VPC
29+
vpc_config = {
30+
security_group_ids = ["sg-028f637312eea735e"]
31+
subnet_ids_to_associate = ["subnet-0b853f8c85796d72d", "subnet-07c068b4b51262793", "subnet-0362f68c559ef7716"]
32+
}
33+
dead_letter_target_arn = "arn:aws:sns:ap-southeast-1:557291035693:demo" # To send failed processing to target, Default is `""`
34+
35+
# IAM
36+
is_create_lambda_role = true # Default is `true`
37+
lambda_role_arn = "" # If `is_create_lambda_role` is `false`
38+
# The policies that you want to attach to IAM Role created by only this module # If `is_create_lambda_role` is `false`
39+
additional_lambda_role_policy_arns = {
40+
allow_lambda_to_read_s3 = "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
41+
}
42+
43+
# Resource policy
44+
lambda_permission_configurations = {
45+
lambda_on_my_account = {
46+
pricipal = "apigateway.amazonaws.com"
47+
source_arn = "arn:aws:execute-api:ap-southeast-1:557291035112:lk36vflbha/*/*/"
48+
}
49+
lambda_on_my_another_account_wrong = {
50+
pricipal = "apigateway.amazonaws.com"
51+
source_arn = "arn:aws:execute-api:ap-southeast-1:224563527112:q6pwa6wgr6/*/*/"
52+
source_account = "557291035112"
53+
}
54+
lambda_on_my_another_account_correct = {
55+
pricipal = "apigateway.amazonaws.com"
56+
source_arn = "arn:aws:execute-api:ap-southeast-1:557291035112:wpj4t3scmb/*/*/"
57+
}
58+
}
59+
60+
# Logging
61+
is_create_cloudwatch_log_group = true # Default is `true`
62+
cloudwatch_log_retention_in_days = 90 # Default is `90`
63+
64+
# Env
65+
ssm_params = {}
66+
plaintext_params = {
67+
region = "ap-southeast-1"
68+
cluster_name = "oozou-dev-test-schedule-cluster"
69+
nodegroup_name = "oozou-dev-test-schedule-custom-nodegroup"
70+
min = 1,
71+
max = 1,
72+
desired = 1
73+
}
74+
75+
tags = var.custom_tags
76+
}

examples/complete/outputs.tf

Whitespace-only changes.

examples/complete/src/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var http = require('http')
2+
3+
exports.handler = (event, context, callback) => {
4+
const options = {
5+
hostname: event.Host,
6+
port: event.Port
7+
}
8+
9+
const response = {};
10+
11+
http.get(options, (res) => {
12+
response.httpStatus = res.statusCode
13+
callback(null, response)
14+
}).on('error', (err) => {
15+
callback(null, err.message);
16+
})
17+
18+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
prefix = "example"
2+
environment = "devops"
3+
name = "cms"
4+
custom_tags = {
5+
"Remark" = "terraform-aws-lambda-example"
6+
}

examples/complete/variables.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* -------------------------------------------------------------------------- */
2+
/* Generics */
3+
/* -------------------------------------------------------------------------- */
4+
variable "prefix" {
5+
description = "The prefix name of customer to be displayed in AWS console and resource"
6+
type = string
7+
}
8+
9+
variable "environment" {
10+
description = "Environment Variable used as a prefix"
11+
type = string
12+
}
13+
14+
variable "name" {
15+
description = "Name of the ECS cluster and s3 also redis to create"
16+
type = string
17+
}
18+
19+
variable "custom_tags" {
20+
description = "Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys"
21+
type = map(any)
22+
default = {}
23+
}

examples/complete/version.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.0.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.0.0"
8+
}
9+
}
10+
}

examples/simple/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Requirements
3+
4+
| Name | Version |
5+
|---------------------------------------------------------------------------|----------|
6+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
7+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0 |
8+
9+
## Providers
10+
11+
No providers.
12+
13+
## Modules
14+
15+
| Name | Source | Version |
16+
|--------------------------------------------------------|--------|---------|
17+
| <a name="module_lambda"></a> [lambda](#module\_lambda) | ../../ | n/a |
18+
19+
## Resources
20+
21+
No resources.
22+
23+
## Inputs
24+
25+
| Name | Description | Type | Default | Required |
26+
|-----------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|------------|---------|:--------:|
27+
| <a name="input_custom_tags"></a> [custom\_tags](#input\_custom\_tags) | Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys | `map(any)` | `{}` | no |
28+
| <a name="input_environment"></a> [environment](#input\_environment) | Environment Variable used as a prefix | `string` | n/a | yes |
29+
| <a name="input_name"></a> [name](#input\_name) | Name of the ECS cluster and s3 also redis to create | `string` | n/a | yes |
30+
| <a name="input_prefix"></a> [prefix](#input\_prefix) | The prefix name of customer to be displayed in AWS console and resource | `string` | n/a | yes |
31+
32+
## Outputs
33+
34+
No outputs.
35+
<!-- END_TF_DOCS -->

examples/simple/main.tf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module "lambda" {
2+
source = "../../"
3+
4+
prefix = var.prefix
5+
environment = var.environment
6+
name = var.name
7+
8+
source_code_dir = "./src"
9+
file_globs = ["index.js"]
10+
compressed_local_file_dir = "./outputs"
11+
12+
runtime = "nodejs12.x"
13+
handler = "index.handler"
14+
15+
additional_lambda_role_policy_arns = {
16+
allow_lambda_to_read_s3 = "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
17+
}
18+
lambda_permission_configurations = {
19+
lambda_on_my_account = {
20+
pricipal = "apigateway.amazonaws.com"
21+
source_arn = "arn:aws:execute-api:ap-southeast-1:557291035112:lk36vflbha/*/*/"
22+
}
23+
lambda_on_my_another_account_wrong = {
24+
pricipal = "apigateway.amazonaws.com"
25+
source_arn = "arn:aws:execute-api:ap-southeast-1:224563527112:q6pwa6wgr6/*/*/"
26+
source_account = "557291035112"
27+
}
28+
lambda_on_my_another_account_correct = {
29+
pricipal = "apigateway.amazonaws.com"
30+
source_arn = "arn:aws:execute-api:ap-southeast-1:557291035112:wpj4t3scmb/*/*/"
31+
}
32+
}
33+
34+
ssm_params = {}
35+
plaintext_params = {
36+
region = "ap-southeast-1"
37+
cluster_name = "oozou-dev-test-schedule-cluster"
38+
nodegroup_name = "oozou-dev-test-schedule-custom-nodegroup"
39+
min = 1,
40+
max = 1,
41+
desired = 1
42+
}
43+
44+
tags = var.custom_tags
45+
}

examples/simple/outputs.tf

Whitespace-only changes.

examples/simple/src/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var http = require('http')
2+
3+
exports.handler = (event, context, callback) => {
4+
const options = {
5+
hostname: event.Host,
6+
port: event.Port
7+
}
8+
9+
const response = {};
10+
11+
http.get(options, (res) => {
12+
response.httpStatus = res.statusCode
13+
callback(null, response)
14+
}).on('error', (err) => {
15+
callback(null, err.message);
16+
})
17+
18+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
prefix = "example"
2+
environment = "devops"
3+
name = "cms"
4+
custom_tags = {
5+
"Remark" = "terraform-aws-lambda-example"
6+
}

examples/simple/variables.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* -------------------------------------------------------------------------- */
2+
/* Generics */
3+
/* -------------------------------------------------------------------------- */
4+
variable "prefix" {
5+
description = "The prefix name of customer to be displayed in AWS console and resource"
6+
type = string
7+
}
8+
9+
variable "environment" {
10+
description = "Environment Variable used as a prefix"
11+
type = string
12+
}
13+
14+
variable "name" {
15+
description = "Name of the ECS cluster and s3 also redis to create"
16+
type = string
17+
}
18+
19+
variable "custom_tags" {
20+
description = "Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys"
21+
type = map(any)
22+
default = {}
23+
}

examples/simple/version.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.0.0"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.0.0"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)