-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathartifacts.tf
More file actions
126 lines (102 loc) · 2.92 KB
/
Copy pathartifacts.tf
File metadata and controls
126 lines (102 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
locals {
artifacts_bucket_name = "artifacts-${local.resource_name}"
}
resource "aws_s3_bucket" "artifacts" {
#bridgecrew:skip=CKV_AWS_144: "Ensure that S3 bucket has cross-region replication enabled". These artifacts are only used by lambda.
#bridgecrew:skip=CKV_AWS_18: "Ensure the S3 bucket has access logging enabled".
bucket = local.artifacts_bucket_name
tags = local.tags
force_destroy = true
object_lock_enabled = true
}
resource "aws_s3_bucket_lifecycle_configuration" "artifacts" {
bucket = aws_s3_bucket.artifacts.id
rule {
id = "AbortFailed"
status = "Enabled"
filter {}
abort_incomplete_multipart_upload {
days_after_initiation = 7
}
}
}
resource "aws_s3_bucket_notification" "bucket_notification" {
bucket = aws_s3_bucket.artifacts.id
eventbridge = true
}
resource "aws_s3_bucket_public_access_block" "artifacts" {
bucket = aws_s3_bucket.artifacts.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_versioning" "versioning_example" {
bucket = aws_s3_bucket.artifacts.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_acl" "artifacts" {
depends_on = [aws_s3_bucket_ownership_controls.default]
bucket = aws_s3_bucket.artifacts.id
acl = "private"
}
resource "aws_s3_bucket_ownership_controls" "default" {
bucket = aws_s3_bucket.artifacts.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "artifacts" {
bucket = aws_s3_bucket.artifacts.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
}
}
}
/*
NOTE: We added this file to the module
Uncomment if you want to regenerate placeholder.zip
data "archive_file" "placeholder" {
output_path = "placeholder.zip"
type = "zip"
source {
filename = "index.js"
content = <<EOF
exports.handler = async (event) => {
const response = {
statusCode: 200,
body: JSON.stringify('Welcome to Nullstone!'),
};
return response;
};
EOF
}
}
locals {
placeholder_path = data.archive_file.placeholder.output_path
placeholder_etag = data.archive_file.placeholder.output_md5
}
*/
locals {
placeholder_path = "placeholder.zip"
placeholder_etag = filemd5(local.placeholder_path)
}
data "aws_s3_objects" "find_existing" {
bucket = aws_s3_bucket.artifacts.bucket
prefix = local.artifact_key
}
locals {
artifact_key = "service-${local.app_version}.zip"
has_artifact = length(data.aws_s3_objects.find_existing.keys) > 0
}
// Add a placeholder object if it doesn't exit
resource "aws_s3_object" "placeholder" {
bucket = aws_s3_bucket.artifacts.bucket
key = "placeholder.zip"
source = local.placeholder_path
etag = local.placeholder_etag
lifecycle { ignore_changes = [etag] }
}