Skip to content

Commit 33be705

Browse files
authored
Merge pull request #13 from highwingio/ianbender/remove-old-deployment-buckets
Clear out unneeded S3 resource
2 parents 335a3b0 + 5576a24 commit 33be705

File tree

3 files changed

+18
-77
lines changed

3 files changed

+18
-77
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ No modules.
4141
| [aws_cloudwatch_log_group.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
4242
| [aws_cloudwatch_metric_alarm.lambda_errors](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
4343
| [aws_iam_role_policy_attachment.lambda_basic_execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
44+
| [aws_iam_role_policy_attachment.lambda_insights](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
4445
| [aws_iam_role_policy_attachment.lambda_networking](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
4546
| [aws_iam_role_policy_attachment.lambda_xray](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
4647
| [aws_lambda_function.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
47-
| [aws_s3_bucket.lambda_deploy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
4848
| [aws_s3_bucket_object.lambda_deploy_object](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_object) | resource |
49-
| [aws_s3_bucket_policy.lambda_deploy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
50-
| [aws_s3_bucket_public_access_block.lambda_deploy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
5149
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
52-
| [aws_iam_policy_document.lambda_deploy_bucket_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
50+
| [aws_ssm_parameter.deployment_bucket_id](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter) | data source |
5351

5452
## Inputs
5553

5654
| Name | Description | Type | Default | Required |
5755
|------|-------------|------|---------|:--------:|
56+
| <a name="input_deployment_bucket_id"></a> [deployment\_bucket\_id](#input\_deployment\_bucket\_id) | ID of S3 bucket that should store our deployment artifacts. Will use the /account/DEPLOYMENT\_BUCKET\_ID value from SSM unless specified otherwise. | `string` | `null` | no |
5857
| <a name="input_description"></a> [description](#input\_description) | Description of the Lambda Function | `string` | `null` | no |
5958
| <a name="input_environment"></a> [environment](#input\_environment) | Environment variables to be passed to the function | `map(string)` | `{}` | no |
6059
| <a name="input_error_rate_alarm_threshold"></a> [error\_rate\_alarm\_threshold](#input\_error\_rate\_alarm\_threshold) | Error rate (in percent, 1-100) at which to trigger an alarm notification | `number` | `25` | no |
60+
| <a name="input_git_sha"></a> [git\_sha](#input\_git\_sha) | Hash generated by `git hash-object` in source repo and used to determine whether a lambda needs to be updated | `string` | `null` | no |
6161
| <a name="input_handler"></a> [handler](#input\_handler) | Name of the handler function inside the artifact (https://docs.aws.amazon.com/lambda/latest/dg/configuration-console.html) | `string` | n/a | yes |
6262
| <a name="input_layer_arns"></a> [layer\_arns](#input\_layer\_arns) | List of ARNs for layers to use with the function | `list(string)` | `[]` | no |
6363
| <a name="input_log_retention_in_days"></a> [log\_retention\_in\_days](#input\_log\_retention\_in\_days) | Number of days to keep function logs in Cloudwatch | `number` | `365` | no |

main.tf

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
*/
2424

2525
locals {
26-
deploy_artifact_key = "deploy.zip"
27-
source_hash = coalesce(var.git_sha, filebase64sha256(var.path))
28-
role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${var.role_name}"
26+
deploy_artifact_key = "deploy.zip"
27+
deployment_bucket_id = coalesce(var.deployment_bucket_id, data.aws_ssm_parameter.deployment_bucket_id.value)
28+
source_hash = coalesce(var.git_sha, filebase64sha256(var.path))
29+
role_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${var.role_name}"
2930
}
3031

3132
# Configure default role permissions
@@ -49,75 +50,9 @@ resource "aws_iam_role_policy_attachment" "lambda_insights" {
4950
policy_arn = "arn:aws:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy"
5051
}
5152

52-
# S3 bucket for deploying Lambda artifacts
53-
# Not always required but it's more consistent for deploying larger files
54-
resource "aws_s3_bucket" "lambda_deploy" {
55-
acl = "private"
56-
bucket_prefix = "lambda-deploy"
57-
force_destroy = true
58-
59-
versioning {
60-
enabled = true
61-
}
62-
63-
server_side_encryption_configuration {
64-
rule {
65-
apply_server_side_encryption_by_default {
66-
sse_algorithm = "AES256"
67-
}
68-
}
69-
}
70-
71-
tags = var.tags
72-
}
73-
74-
# Block public access to the deploy artifacts
75-
resource "aws_s3_bucket_public_access_block" "lambda_deploy" {
76-
bucket = aws_s3_bucket.lambda_deploy.id
77-
78-
block_public_acls = true
79-
block_public_policy = true
80-
ignore_public_acls = true
81-
restrict_public_buckets = true
82-
}
83-
84-
# Deny insecure requests for deploy artifacts
85-
data "aws_iam_policy_document" "lambda_deploy_bucket_policy" {
86-
statement {
87-
sid = "OnlyAllowSSLRequests"
88-
89-
actions = [
90-
"s3:*",
91-
]
92-
93-
effect = "Deny"
94-
95-
resources = [
96-
aws_s3_bucket.lambda_deploy.arn,
97-
"${aws_s3_bucket.lambda_deploy.arn}/*",
98-
]
99-
100-
condition {
101-
test = "Bool"
102-
variable = "aws:SecureTransport"
103-
values = ["false"]
104-
}
105-
106-
principals {
107-
type = "*"
108-
identifiers = ["*"]
109-
}
110-
}
111-
}
112-
113-
resource "aws_s3_bucket_policy" "lambda_deploy" {
114-
bucket = aws_s3_bucket.lambda_deploy.id
115-
policy = data.aws_iam_policy_document.lambda_deploy_bucket_policy.json
116-
}
117-
11853
# S3 object to hold the deployed artifact
11954
resource "aws_s3_bucket_object" "lambda_deploy_object" {
120-
bucket = data.aws_ssm_parameter.deployment_bucket_id.value
55+
bucket = local.deployment_bucket_id
12156
key = "${var.name}/${local.deploy_artifact_key}"
12257
source = var.path
12358
source_hash = md5(local.source_hash)
@@ -140,7 +75,7 @@ resource "aws_lambda_function" "lambda" {
14075
reserved_concurrent_executions = var.reserved_concurrent_executions
14176
role = local.role_arn
14277
runtime = var.runtime
143-
s3_bucket = data.aws_ssm_parameter.deployment_bucket_id.value
78+
s3_bucket = local.deployment_bucket_id
14479
s3_key = aws_s3_bucket_object.lambda_deploy_object.key
14580
s3_object_version = aws_s3_bucket_object.lambda_deploy_object.version_id
14681
tags = var.tags

vars.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ variable "description" {
44
type = string
55
}
66

7+
variable "deployment_bucket_id" {
8+
default = null
9+
description = "ID of S3 bucket that should store our deployment artifacts. Will use the /account/DEPLOYMENT_BUCKET_ID value from SSM unless specified otherwise."
10+
type = string
11+
}
12+
713
variable "environment" {
814
default = {}
915
description = "Environment variables to be passed to the function"
@@ -17,9 +23,9 @@ variable "error_rate_alarm_threshold" {
1723
}
1824

1925
variable "git_sha" {
20-
type = string
21-
description = "Git SHA hash for lambda source code"
2226
default = null
27+
description = "Hash generated by `git hash-object` in source repo and used to determine whether a lambda needs to be updated"
28+
type = string
2329
}
2430

2531
variable "handler" {

0 commit comments

Comments
 (0)