Skip to content

Commit 8652e1e

Browse files
authored
Merge pull request #362 from lorengordon/lambda-new
2 parents 531a9a3 + 9428bb9 commit 8652e1e

19 files changed

+165
-57
lines changed

.bumpversion.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.4.2
2+
current_version = 1.0.0
33
commit = True
44
message = Bumps version to {new_version}
55
tag = False

.github/dependabot.yml

-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ updates:
55
schedule:
66
interval: weekly
77
open-pull-requests-limit: 10
8-
- package-ecosystem: pip
9-
directory: "lambda/tests/"
10-
schedule:
11-
interval: weekly
12-
open-pull-requests-limit: 10
13-
- package-ecosystem: pip
14-
directory: "tests/"
15-
schedule:
16-
interval: weekly
17-
open-pull-requests-limit: 10
188
- package-ecosystem: pip
199
directory: "/"
2010
schedule:

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ tests/go.*
2222
# python cache
2323
__pycache__
2424
.python-version
25+
.pytest_cache
26+
27+
# lambda builds
28+
builds/

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ jobs:
4646
(set -x; git tag -a $RELEASE_VERSION -m $RELEASE_VERSION)
4747
deploy:
4848
provider: releases
49-
api_key:
50-
secure: ZAqybnZivV40iVEAU5qYsQD2fFoDdwkbHpQwxya6IPyCkt6dUlY46xio4gjl18a2PQ+gUV/uZB72f2OOHSnwIkzPQkSXOc+DMSkYzDuBfmvTChZZ9lTVt4gyh6NoAkIIJquHP36JotacvO/dkVNaORc5zMxm+UCc28fB8QjCIc9cMuI9kpHGtza4qvGLCKs7EGMbSTvrffiUSiY2Vwm/HBK2VwU9C48TxC100PDo09JjXvIYEzfICuhR+3u7igmIWrHtMoNJ/r540tEsJD8XUrXSdO18DpqEIFd7J2qEMifN7kmP+Y8zxW2U4jx1bYHCy14uhNWBHE2mgjrcMbgS7TLcMi6UUlekDIYxWq7NHmf9n1YFGMfBhDXGiI/LgglGM9RzgXXk6jC1ptQAb1tUIWHc2294j53fEkecoIifq/zD4aqZ0/O9jj74vCF2NhcOKiZchsXuUDRPg3Vc/PJAC4taehgPr2ift4R7jOSSAMRQV4Gznc7FkwAWs9PCyCQQ3IJ0+qPxawcqz7iSXENb1ZZLaNf0hcD1CtyRQ/7oDKGwfNcn0Q6TQeCSBy85QTtclTwCurHrJ7Ct7egIzU3dHWuwOdx4URghJzothC+r8cQj0nvZuH4gkD8TUpHU82CKpdx9sh0ArRkKaFf41xHvKJFBoQ6HyxTHw3SzpuhFAbI=
49+
api_key: $GH_RELEASES_TOKEN
5150
name: $RELEASE_VERSION
5251
body: $RELEASE_BODY
5352
tag_name: $RELEASE_VERSION

CHANGELOG.md

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
### 1.0.0
8+
9+
**Commit Delta**: [Change from 0.4.2 release](https://github.com/plus3it/terraform-aws-org-new-account-trust-policy/compare/0.4.2...1.0.0)
10+
11+
**Released**: 2022.10.14
12+
13+
**Summary**:
14+
15+
* Changed lambda module to one published by terraform-aws-modules, for better long-term support
16+
17+
* Exposed new `lambda` variable that wraps arguments for the upstream lambda module
18+
19+
* Added support for creating multiple instances of this module. This achieved by either:
20+
* Tailoring the artifact location, by setting `lambda.artifacts_dir` to a different location for each instance
21+
* Creating the package separately from the lambda functions, see `tests/test_create_package_separately` for an example
22+
723
### 0.4.2
824

925
**Commit Delta**: [Change from 0.4.1 release](https://github.com/plus3it/terraform-aws-org-new-account-trust-policy/compare/0.4.1...0.4.2)

Dockerfile

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
FROM plus3it/tardigrade-ci:0.23.2
22

3-
COPY ./lambda/src/requirements.txt /lambda/src/requirements.txt
4-
COPY ./lambda/tests/requirements_dev.txt /lambda/tests/requirements_dev.txt
5-
COPY ./tests/requirements_test.txt /tests/requirements_test.txt
6-
COPY ./requirements_common.txt /requirements_common.txt
3+
COPY ./lambda/src/requirements.txt /app/requirements.txt
4+
COPY ./requirements/requirements_dev.txt /app/requirements_dev.txt
5+
COPY ./requirements/requirements_test.txt /app/requirements_test.txt
6+
COPY ./requirements/requirements_common.txt /app/requirements_common.txt
77

88
RUN python -m pip install --no-cache-dir \
9-
-r /lambda/src/requirements.txt \
10-
-r /lambda/tests/requirements_dev.txt \
11-
-r /tests/requirements_test.txt
9+
-r /app/requirements.txt \
10+
-r /app/requirements_dev.txt \
11+
-r /app/requirements_test.txt

Dockerfile_test

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
FROM plus3it/tardigrade-ci:0.23.2
22

3-
COPY ./tests/requirements_test.txt /tests/requirements_test.txt
4-
COPY ./requirements_common.txt /requirements_common.txt
3+
USER root
54

6-
RUN python -m pip install --no-cache-dir -r /tests/requirements_test.txt
5+
#Set of all dependencies needed for pyenv to work on Ubuntu
6+
RUN apt-get update \
7+
&& apt-get install -y --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget ca-certificates curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev mecab-ipadic-utf8 git
8+
9+
USER tardigrade-ci
10+
11+
# Set-up necessary Env vars for PyEnv
12+
ENV PYTHON_VERSION 3.8.10
13+
ENV PYENV_ROOT /home/tardigrade-ci/.pyenv
14+
ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
15+
16+
# Install pyenv
17+
RUN curl https://pyenv.run | bash \
18+
&& pyenv update \
19+
&& pyenv install $PYTHON_VERSION \
20+
&& pyenv rehash \
21+
&& pyenv global system ${PYTHON_VERSION}
22+
23+
COPY ./requirements/requirements_test.txt /app/requirements_test.txt
24+
COPY ./requirements/requirements_common.txt /app/requirements_common.txt
25+
26+
RUN python -m pip install --no-cache-dir -r /app/requirements_test.txt

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ make mockstack/clean
3737

3838
| Name | Version |
3939
|------|---------|
40-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.12 |
40+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
41+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.9 |
42+
| <a name="requirement_external"></a> [external](#requirement\_external) | >= 1.0 |
43+
| <a name="requirement_local"></a> [local](#requirement\_local) | >= 1.0 |
44+
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2.0 |
45+
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3.0 |
4146

4247
## Providers
4348

4449
| Name | Version |
4550
|------|---------|
46-
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
47-
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
51+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.9 |
52+
| <a name="provider_random"></a> [random](#provider\_random) | >= 3.0 |
4853

4954
## Resources
5055

@@ -61,6 +66,7 @@ make mockstack/clean
6166
| <a name="input_role_name"></a> [role\_name](#input\_role\_name) | Name of the IAM role to create in the target account (case sensitive) | `string` | n/a | yes |
6267
| <a name="input_role_permission_policy"></a> [role\_permission\_policy](#input\_role\_permission\_policy) | AWS-managed permission policy name to attach to the role (case sensitive) | `string` | n/a | yes |
6368
| <a name="input_trust_policy_json"></a> [trust\_policy\_json](#input\_trust\_policy\_json) | JSON-formatted string containing the role trust policy | `string` | n/a | yes |
69+
| <a name="input_lambda"></a> [lambda](#input\_lambda) | Map of any additional arguments for the upstream lambda module. See <https://github.com/terraform-aws-modules/terraform-aws-lambda> | `any` | `{}` | no |
6470
| <a name="input_log_level"></a> [log\_level](#input\_log\_level) | Log level of the lambda output, one of: debug, info, warning, error, critical | `string` | `"info"` | no |
6571
| <a name="input_tags"></a> [tags](#input\_tags) | Tags that are passed to resources | `map(string)` | `{}` | no |
6672

lambda/tests/requirements_dev.txt

-2
This file was deleted.

main.tf

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
2-
terraform {
3-
required_version = ">= 0.12"
4-
}
5-
61
locals {
72
name = "new_account_iam_role_${random_string.id.result}"
83
}
@@ -32,25 +27,32 @@ data "aws_iam_policy_document" "lambda" {
3227
}
3328

3429
module "lambda" {
35-
source = "git::https://github.com/plus3it/terraform-aws-lambda.git?ref=v1.3.0"
30+
source = "git::https://github.com/terraform-aws-modules/terraform-aws-lambda.git?ref=v4.1.1"
3631

3732
function_name = local.name
38-
description = "Create new IAM Account Role"
39-
handler = "new_account_iam_role.lambda_handler"
40-
policy = data.aws_iam_policy_document.lambda
41-
runtime = "python3.8"
42-
source_path = "${path.module}/lambda/src"
43-
tags = var.tags
44-
timeout = 300
45-
46-
environment = {
47-
variables = {
48-
ASSUME_ROLE_NAME = var.assume_role_name
49-
ROLE_NAME = var.role_name
50-
PERMISSION_POLICY = var.role_permission_policy
51-
TRUST_POLICY_JSON = var.trust_policy_json
52-
LOG_LEVEL = var.log_level
53-
}
33+
34+
description = "Create new IAM Account Role"
35+
handler = "new_account_iam_role.lambda_handler"
36+
runtime = "python3.8"
37+
timeout = 300
38+
tags = var.tags
39+
40+
attach_policy_json = true
41+
policy_json = data.aws_iam_policy_document.lambda.json
42+
43+
source_path = "${path.module}/lambda/src"
44+
45+
artifacts_dir = try(var.lambda.artifacts_dir, "builds")
46+
create_package = try(var.lambda.create_package, true)
47+
local_existing_package = try(var.lambda.local_existing_package, null)
48+
recreate_missing_package = try(var.lambda.recreate_missing_package, false)
49+
50+
environment_variables = {
51+
ASSUME_ROLE_NAME = var.assume_role_name
52+
ROLE_NAME = var.role_name
53+
PERMISSION_POLICY = var.role_permission_policy
54+
TRUST_POLICY_JSON = var.trust_policy_json
55+
LOG_LEVEL = var.log_level
5456
}
5557
}
5658

@@ -81,12 +83,12 @@ resource "aws_cloudwatch_event_rule" "this" {
8183

8284
resource "aws_cloudwatch_event_target" "this" {
8385
rule = aws_cloudwatch_event_rule.this.name
84-
arn = module.lambda.function_arn
86+
arn = module.lambda.lambda_function_arn
8587
}
8688

8789
resource "aws_lambda_permission" "events" {
8890
action = "lambda:InvokeFunction"
89-
function_name = module.lambda.function_name
91+
function_name = module.lambda.lambda_function_name
9092
principal = "events.amazonaws.com"
9193
source_arn = aws_cloudwatch_event_rule.this.arn
9294
}
File renamed without changes.

requirements/requirements_dev.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
moto==3.1.18
2+
-r ./requirements_common.txt
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
tftest==1.7.4
22
localstack-client==1.39
3-
-r ../requirements_common.txt
3+
-r ./requirements_common.txt

tests/localstack.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ provider "aws" {
55
skip_credentials_validation = true
66
skip_metadata_api_check = true
77
skip_requesting_account_id = true
8-
s3_force_path_style = true
8+
s3_use_path_style = true
99

1010
endpoints {
1111
cloudwatch = "http://${var.localstack_host}:4566"

tests/test_create_all/main.tf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module "test_create_all" {
2+
source = "../.."
3+
4+
assume_role_name = "FOO"
5+
trust_policy_json = jsonencode({})
6+
role_name = "BAR"
7+
role_permission_policy = "ReadOnlyAccess"
8+
log_level = "Info"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module "test_create_package" {
2+
source = "git::https://github.com/terraform-aws-modules/terraform-aws-lambda.git?ref=v4.1.1"
3+
4+
create_function = false
5+
create_package = true
6+
7+
recreate_missing_package = false
8+
9+
runtime = "python3.8"
10+
source_path = "${path.module}/../../lambda/src"
11+
}
12+
13+
module "test_create_function" {
14+
source = "../.."
15+
16+
assume_role_name = "FOO"
17+
trust_policy_json = jsonencode({})
18+
role_name = "BAR"
19+
role_permission_policy = "ReadOnlyAccess"
20+
log_level = "Info"
21+
22+
lambda = {
23+
local_existing_package = "${path.module}/${module.test_create_package.local_filename}"
24+
create_package = false
25+
}
26+
}

tests/test_terraform_install.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def test_outputs(tf_output):
140140
prefix = "new_account_iam_role"
141141

142142
lambda_module = tf_output["lambda"]
143-
assert lambda_module["function_name"].startswith(prefix)
143+
assert lambda_module["lambda_function_name"].startswith(prefix)
144144

145145
event_rule_output = tf_output["aws_cloudwatch_event_rule"]
146146
assert event_rule_output["name"].startswith(prefix)
@@ -157,7 +157,7 @@ def test_lambda_dry_run(tf_output, localstack_session):
157157
lambda_client = localstack_session.client("lambda", region_name=AWS_DEFAULT_REGION)
158158
lambda_module = tf_output["lambda"]
159159
response = lambda_client.invoke(
160-
FunctionName=lambda_module["function_name"],
160+
FunctionName=lambda_module["lambda_function_name"],
161161
InvocationType="DryRun",
162162
)
163163
assert response["StatusCode"] == 204
@@ -174,7 +174,7 @@ def test_lambda_invocation(tf_output, localstack_session, mock_event):
174174
lambda_client = localstack_session.client("lambda", region_name=AWS_DEFAULT_REGION)
175175
lambda_module = tf_output["lambda"]
176176
response = lambda_client.invoke(
177-
FunctionName=lambda_module["function_name"],
177+
FunctionName=lambda_module["lambda_function_name"],
178178
InvocationType="RequestResponse",
179179
Payload=json.dumps(mock_event),
180180
)

variables.tf

+10
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@ variable "assume_role_name" {
22
description = "Name of IAM role to assume the target account (case sensitive)"
33
type = string
44
}
5+
56
variable "role_name" {
67
description = "Name of the IAM role to create in the target account (case sensitive)"
78
type = string
89
}
10+
911
variable "role_permission_policy" {
1012
description = "AWS-managed permission policy name to attach to the role (case sensitive)"
1113
type = string
1214
}
15+
1316
variable "trust_policy_json" {
1417
description = "JSON-formatted string containing the role trust policy"
1518
type = string
1619
}
20+
21+
variable "lambda" {
22+
description = "Map of any additional arguments for the upstream lambda module. See <https://github.com/terraform-aws-modules/terraform-aws-lambda>"
23+
type = any
24+
default = {}
25+
}
26+
1727
variable "log_level" {
1828
default = "info"
1929
description = "Log level of the lambda output, one of: debug, info, warning, error, critical"

versions.tf

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
terraform {
2+
required_version = ">= 0.13.1"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.9"
8+
}
9+
external = {
10+
source = "hashicorp/external"
11+
version = ">= 1.0"
12+
}
13+
local = {
14+
source = "hashicorp/local"
15+
version = ">= 1.0"
16+
}
17+
null = {
18+
source = "hashicorp/null"
19+
version = ">= 2.0"
20+
}
21+
random = {
22+
source = "hashicorp/random"
23+
version = ">= 3.0"
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)