Skip to content

Commit 8de1566

Browse files
authored
Upgrate to terraform v12 (#11)
* Upgrade to v12 - step1 * Upgrade to v12
1 parent 3ea3b02 commit 8de1566

File tree

12 files changed

+123
-87
lines changed

12 files changed

+123
-87
lines changed

example/acm.tf

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
# ACM Certificate generation
22
resource "aws_acm_certificate" "cert" {
3-
provider = "aws.cloudfront"
4-
domain_name = "${var.fqdn}"
3+
provider = aws.cloudfront
4+
domain_name = var.fqdn
55
validation_method = "DNS"
66
}
77

88
resource "aws_route53_record" "cert_validation" {
9-
provider = "aws.cloudfront"
10-
name = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_name}"
11-
type = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_type}"
12-
zone_id = "${data.aws_route53_zone.main.id}"
13-
records = ["${aws_acm_certificate.cert.domain_validation_options.0.resource_record_value}"]
9+
provider = aws.cloudfront
10+
name = aws_acm_certificate.cert.domain_validation_options[0].resource_record_name
11+
type = aws_acm_certificate.cert.domain_validation_options[0].resource_record_type
12+
zone_id = data.aws_route53_zone.main.id
13+
records = [aws_acm_certificate.cert.domain_validation_options[0].resource_record_value]
1414
ttl = 60
1515
}
1616

1717
resource "aws_acm_certificate_validation" "cert" {
18-
provider = "aws.cloudfront"
19-
certificate_arn = "${aws_acm_certificate.cert.arn}"
20-
validation_record_fqdns = ["${aws_route53_record.cert_validation.fqdn}"]
18+
provider = aws.cloudfront
19+
certificate_arn = aws_acm_certificate.cert.arn
20+
validation_record_fqdns = [aws_route53_record.cert_validation.fqdn]
2121
}
2222

23-
2423
# Route 53 record for the static site
2524

2625
data "aws_route53_zone" "main" {
27-
provider = "aws.main"
28-
name = "${var.cookieDomain}"
26+
provider = aws.main
27+
name = var.cookieDomain
2928
private_zone = false
3029
}
3130

3231
resource "aws_route53_record" "web" {
33-
provider = "aws.main"
34-
zone_id = "${data.aws_route53_zone.main.zone_id}"
35-
name = "${var.fqdn}"
32+
provider = aws.main
33+
zone_id = data.aws_route53_zone.main.zone_id
34+
name = var.fqdn
3635
type = "A"
3736

3837
alias {
39-
name = "${module.main.cf_domain_name}"
40-
zone_id = "${module.main.cf_hosted_zone_id}"
38+
name = module.main.cf_domain_name
39+
zone_id = module.main.cf_hosted_zone_id
4140
evaluate_target_health = false
4241
}
4342
}
43+

example/main.tf

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ module "lambda" {
22
source = "../"
33

44
# default key is "config.json"
5-
bucketName = "${var.bucketName}"
6-
bucketKey = "${var.bucketKey}"
7-
cookieDomain = "${var.cookieDomain}"
5+
bucketName = var.bucketName
6+
bucketKey = var.bucketKey
7+
cookieDomain = var.cookieDomain
88
}
99

10-
1110
module "main" {
12-
// PR for lambda enabled need to merged
13-
// source = "../../terraform-aws-s3-cloudfront-website"
14-
source = "/Users/capitant/working/terraform-aws-s3-cloudfront-website"
11+
source = "../../terraform-aws-s3-cloudfront-website"
12+
13+
// source = "/Users/capitant/working/terraform-aws-s3-cloudfront-website"
1514

16-
fqdn = "${var.fqdn}"
17-
ssl_certificate_arn = "${aws_acm_certificate_validation.cert.certificate_arn}"
18-
allowed_ips = "${var.allowed_ips}"
15+
fqdn = var.fqdn
16+
ssl_certificate_arn = aws_acm_certificate_validation.cert.certificate_arn
17+
allowed_ips = var.allowed_ips
1918

2019
index_document = "index.html"
2120
error_document = "404.html"
2221

23-
refer_secret = "${base64sha512("REFER-SECRET-19265125-${var.fqdn}-52865926")}"
22+
refer_secret = base64sha512("REFER-SECRET-19265125-${var.fqdn}-52865926")
2423

2524
force_destroy = "true"
2625

27-
providers {
28-
"aws.main" = "aws.main"
29-
"aws.cloudfront" = "aws.cloudfront"
26+
providers = {
27+
aws.main = aws.main
28+
aws.cloudfront = aws.cloudfront
3029
}
3130

32-
lambda_edge_enabled = "true"
31+
lambda_edge_enabled = "true"
3332
lambda_edge_arn_version = "${module.lambda.arn}:${module.lambda.version}"
3433
}
34+

example/outputs.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
output "s3_domain" {
2-
value = "${module.main.s3_website_endpoint}"
2+
value = module.main.s3_website_endpoint
33
}
44

55
output "cloudfront_domain" {
6-
value = "${module.main.cf_domain_name}"
6+
value = module.main.cf_domain_name
77
}
88

99
output "cloudfront_hosted_zone_id" {
10-
value = "${module.main.cf_hosted_zone_id}"
10+
value = module.main.cf_hosted_zone_id
1111
}
1212

1313
output "cloudfront_distribution_id" {
14-
value = "${module.main.cf_distribution_id}"
14+
value = module.main.cf_distribution_id
1515
}
1616

1717
output "route53_fqdn" {
18-
value = "${aws_route53_record.web.fqdn}"
18+
value = aws_route53_record.web.fqdn
1919
}
2020

2121
output "acm_certificate_arn" {
22-
value = "${aws_acm_certificate_validation.cert.certificate_arn}"
22+
value = aws_acm_certificate_validation.cert.certificate_arn
2323
}
24+

example/providers.tf

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1+
variable "region" {
2+
description = "fix region empty issue of terraform v12"
3+
type = string
4+
default = "us-east-1"
5+
}
6+
17
# AWS Region for S3 and other resources
28
provider "aws" {
9+
// region = "us-east-1"
310
region = "us-west-2"
4-
alias = "main"
11+
alias = "main"
12+
version = "~> 2.9"
513
}
614

7-
815
# AWS Region for Cloudfront (ACM certs only supports us-east-1)
916
provider "aws" {
1017
region = "us-east-1"
11-
alias = "cloudfront"
18+
alias = "cloudfront"
19+
version = "~> 2.9"
1220
}
1321

22+
23+
24+
provider "aws" {
25+
region = var.region
26+
}

example/s3_access.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
resource "aws_s3_bucket_object" "object" {
2-
bucket = "${var.bucketName}"
3-
key = "${var.bucketKey}"
2+
bucket = var.bucketName
3+
key = var.bucketKey
44
source = "${path.module}/config.json"
5+
provider = aws.main
56
}
7+

example/vars.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ variable "cookieDomain" {
1111

1212
# Allowed IPs that can directly access the S3 bucket
1313
variable "allowed_ips" {
14-
type = "list"
15-
default = [ "0.0.0.0/0" ]
14+
type = list(string)
15+
default = ["0.0.0.0/0"]
1616
}
1717

1818
variable "bucketName" {
19-
default = "mysite.htaccess"
19+
default = "mysite.htaccess2"
2020
}
2121

2222
variable "bucketKey" {
@@ -30,3 +30,4 @@ variable "lambda_basic_username" {
3030
variable "lambda_basic_password" {
3131
default = "test"
3232
}
33+

example/versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
terraform {
3+
required_version = ">= 0.12"
4+
}

main.tf

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
resource "template_file" "this" {
2-
template = "${file("${path.module}/src/params.json")}"
3-
4-
vars {
5-
BUCKET_NAME = "${var.bucketName}"
6-
BUCKET_KEY = "${var.bucketKey}"
7-
COOKIE_DOMAIN = "${var.cookieDomain}"
8-
}
9-
}
1+
//resource "template_file" "this" {
2+
// template = file("${path.module}/src/params.json")
3+
//
4+
// vars = {
5+
// BUCKET_NAME = var.bucketName
6+
// BUCKET_KEY = var.bucketKey
7+
// COOKIE_DOMAIN = var.cookieDomain
8+
// }
9+
//}
1010

1111
resource "local_file" "params" {
12-
content = "${template_file.this.rendered}"
12+
// content = template_file.this.rendered
13+
content = templatefile("${path.module}/src/params.json", {
14+
BUCKET_NAME = var.bucketName
15+
BUCKET_KEY = var.bucketKey
16+
COOKIE_DOMAIN = var.cookieDomain
17+
})
18+
1319
filename = "${path.module}/.archive/params.json"
1420
}
1521

@@ -18,33 +24,34 @@ data "local_file" "mainjs" {
1824
}
1925

2026
resource "local_file" "mainjs" {
21-
content = "${data.local_file.mainjs.content}"
27+
content = data.local_file.mainjs.content
2228
filename = "${path.module}/.archive/main.js"
2329
}
2430

2531
data "archive_file" "this" {
2632
depends_on = [
27-
"local_file.params",
28-
"local_file.mainjs"
33+
local_file.params,
34+
local_file.mainjs,
2935
]
3036

31-
type = "zip"
37+
type = "zip"
3238
output_path = "${path.module}/.archive.zip"
33-
source_dir = "${path.module}/.archive"
39+
source_dir = "${path.module}/.archive"
3440
}
3541

3642
resource "aws_lambda_function" "this" {
3743
description = "Basic HTTP authentication module/function"
38-
role = "${aws_iam_role.this.arn}"
39-
runtime = "nodejs8.10"
44+
role = aws_iam_role.this.arn
45+
runtime = "nodejs8.10"
4046

41-
filename = "${data.archive_file.this.output_path}"
42-
source_code_hash = "${data.archive_file.this.output_base64sha256}"
47+
filename = data.archive_file.this.output_path
48+
source_code_hash = data.archive_file.this.output_base64sha256
4349

44-
function_name = "${var.name}"
45-
handler = "main.handler"
50+
function_name = var.name
51+
handler = "main.handler"
4652

47-
timeout = "${var.fn_timeout}"
48-
memory_size = "${var.fn_memory_size}"
49-
publish = true
53+
timeout = var.fn_timeout
54+
memory_size = var.fn_memory_size
55+
publish = true
5056
}
57+

outputs.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
output "fn_name" {
2-
value = "${aws_lambda_function.this.function_name}"
2+
value = aws_lambda_function.this.function_name
33
}
44

55
output "arn" {
6-
value = "${aws_lambda_function.this.arn}"
6+
value = aws_lambda_function.this.arn
77
}
88

99
output "qualified_arn" {
10-
value = "${aws_lambda_function.this.qualified_arn}"
10+
value = aws_lambda_function.this.qualified_arn
1111
}
1212

1313
output "invoke_arn" {
14-
value = "${aws_lambda_function.this.invoke_arn}"
14+
value = aws_lambda_function.this.invoke_arn
1515
}
1616

1717
output "id" {
18-
value = "${aws_lambda_function.this.id}"
18+
value = aws_lambda_function.this.id
1919
}
2020

2121
output "version" {
22-
value = "${aws_lambda_function.this.version}"
22+
value = aws_lambda_function.this.version
2323
}
24+

sts_role.tf

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ data "aws_iam_policy_document" "this" {
3232
]
3333

3434
resources = [
35-
"arn:aws:logs:*:*:*"
35+
"arn:aws:logs:*:*:*",
3636
]
3737
}
3838

3939
statement {
4040
actions = [
41-
"s3:GetObject"
41+
"s3:GetObject",
4242
]
4343
resources = [
44-
"arn:aws:s3:::${var.bucketName}/*"
44+
"arn:aws:s3:::${var.bucketName}/*",
4545
]
4646
}
4747

@@ -53,18 +53,19 @@ data "aws_iam_policy_document" "this" {
5353
]
5454

5555
resources = [
56-
"${aws_lambda_function.this.arn}",
56+
aws_lambda_function.this.arn,
5757
]
5858
}
5959
}
6060

6161
resource "aws_iam_role_policy" "this" {
62-
name = "${var.name}"
63-
role = "${aws_iam_role.this.id}"
64-
policy = "${data.aws_iam_policy_document.this.json}"
62+
name = var.name
63+
role = aws_iam_role.this.id
64+
policy = data.aws_iam_policy_document.this.json
6565
}
6666

6767
resource "aws_iam_role" "this" {
68-
name = "${var.name}"
69-
assume_role_policy = "${data.aws_iam_policy_document.sts.json}"
68+
name = var.name
69+
assume_role_policy = data.aws_iam_policy_document.sts.json
7070
}
71+

0 commit comments

Comments
 (0)