Skip to content

Commit def6263

Browse files
authored
Merge pull request #99 from ruby-no-kai/tfmod
terraform module ruby-no-kai/rubykaigi-net#210
2 parents 6cb35b8 + 0892f54 commit def6263

24 files changed

+383
-412
lines changed

tf/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

tf/.mairu.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

tf/.terraform.lock.hcl

Lines changed: 0 additions & 45 deletions
This file was deleted.

tf/acm.tf

Lines changed: 0 additions & 10 deletions
This file was deleted.

tf/apprunner.tf

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
data "external" "apprunner-deploy" {
2-
program = ["ruby", "${path.module}/apprunner_deploy.rb"]
3-
}
1+
# Note: apprunner_deploy.rb script handling is deferred
2+
# Image identifier and environment variables should be managed outside Terraform
3+
# Using lifecycle ignore_changes to prevent Terraform from reverting manual updates
4+
5+
resource "aws_apprunner_service" "main" {
6+
count = var.enable_apprunner ? 1 : 0
47

5-
resource "aws_apprunner_service" "prd" {
6-
service_name = "sponsor-app"
8+
service_name = var.service_name
79

810
source_configuration {
911
image_repository {
1012
image_configuration {
1113
port = "3000"
12-
runtime_environment_variables = merge(jsondecode(data.external.apprunner-deploy.result.runtime_environment_variables), {
13-
})
14-
runtime_environment_secrets = jsondecode(data.external.apprunner-deploy.result.runtime_environment_secrets)
14+
runtime_environment_variables = {}
15+
runtime_environment_secrets = {}
1516
}
16-
image_identifier = data.external.apprunner-deploy.result.image_identifier
17+
image_identifier = "${var.enable_shared_resources ? aws_ecr_repository.app[0].repository_url : "005216166247.dkr.ecr.us-west-2.amazonaws.com/sponsor-app"}:latest"
1718
image_repository_type = "ECR"
1819
}
1920
authentication_configuration {
20-
access_role_arn = aws_iam_role.app-runner-access.arn
21+
access_role_arn = aws_iam_role.app-runner-access[0].arn
2122
}
2223
auto_deployments_enabled = false
2324
}
@@ -38,18 +39,30 @@ resource "aws_apprunner_service" "prd" {
3839
}
3940

4041
tags = {
41-
Name = "sponsor-app"
42-
Environment = "production"
42+
Name = var.service_name
43+
Environment = var.environment
44+
}
45+
46+
lifecycle {
47+
ignore_changes = [
48+
source_configuration[0].image_repository[0].image_identifier,
49+
source_configuration[0].image_repository[0].image_configuration[0].runtime_environment_variables,
50+
source_configuration[0].image_repository[0].image_configuration[0].runtime_environment_secrets,
51+
]
4352
}
4453
}
4554

4655
resource "aws_iam_role" "app-runner-access" {
47-
name = "AppraSponsorApp"
48-
description = "prd tf/iam.tf"
49-
assume_role_policy = data.aws_iam_policy_document.app-runner-access-trust.json
56+
count = var.enable_apprunner ? 1 : 0
57+
58+
name = var.iam_apprunner_access_name
59+
description = "${var.environment} tf/iam.tf"
60+
assume_role_policy = data.aws_iam_policy_document.app-runner-access-trust[0].json
5061
}
5162

5263
data "aws_iam_policy_document" "app-runner-access-trust" {
64+
count = var.enable_apprunner ? 1 : 0
65+
5366
statement {
5467
effect = "Allow"
5568
actions = ["sts:AssumeRole"]
@@ -63,11 +76,15 @@ data "aws_iam_policy_document" "app-runner-access-trust" {
6376
}
6477

6578
resource "aws_iam_role_policy" "app-runner-access" {
66-
role = aws_iam_role.app-runner-access.name
67-
policy = data.aws_iam_policy_document.app-runner-access.json
79+
count = var.enable_apprunner ? 1 : 0
80+
81+
role = aws_iam_role.app-runner-access[0].name
82+
policy = data.aws_iam_policy_document.app-runner-access[0].json
6883
}
6984

7085
data "aws_iam_policy_document" "app-runner-access" {
86+
count = var.enable_apprunner ? 1 : 0
87+
7188
statement {
7289
effect = "Allow"
7390
actions = [
@@ -85,8 +102,7 @@ data "aws_iam_policy_document" "app-runner-access" {
85102
"ecr:DescribeImages",
86103
]
87104
resources = [
88-
aws_ecr_repository.app.arn,
105+
var.enable_shared_resources ? aws_ecr_repository.app[0].arn : "arn:aws:ecr:us-west-2:005216166247:repository/sponsor-app",
89106
]
90107
}
91108
}
92-

tf/apprunner_deploy.rb

Lines changed: 0 additions & 16 deletions
This file was deleted.

tf/aws.tf

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
1-
data "aws_caller_identity" "current" {}
2-
3-
locals {
4-
aws_account_id = "005216166247"
5-
}
1+
data "aws_region" "current" {}
62

7-
provider "aws" {
8-
region = "us-west-2"
9-
allowed_account_ids = [local.aws_account_id]
10-
default_tags {
11-
tags = {
12-
Project = "sponsor-app"
13-
}
14-
}
15-
}
16-
17-
provider "aws" {
18-
alias = "use1"
19-
region = "us-east-1"
20-
allowed_account_ids = [local.aws_account_id]
21-
default_tags {
22-
tags = {
23-
Project = "sponsor-app"
24-
}
25-
}
26-
}
3+
data "aws_caller_identity" "current" {}
274

28-
provider "aws" {
29-
alias = "apne1"
30-
region = "ap-northeast-1"
31-
allowed_account_ids = [local.aws_account_id]
32-
default_tags {
33-
tags = {
34-
Project = "sponsor-app"
35-
}
36-
}
37-
}
5+
data "aws_default_tags" "current" {}

tf/backend.tf

Lines changed: 0 additions & 14 deletions
This file was deleted.

tf/cloudfront.tf

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
1-
resource "aws_cloudfront_distribution" "prd" {
2-
comment = "sponsor-app"
1+
resource "aws_cloudfront_distribution" "main" {
2+
count = var.enable_cloudfront ? 1 : 0
3+
4+
provider = aws.cloudfront
5+
6+
comment = var.cloudfront_comment != "" ? var.cloudfront_comment : "sponsor-app"
37

48
enabled = true
59
is_ipv6_enabled = true
610
http_version = "http2and3"
711
price_class = "PriceClass_All"
812

9-
aliases = ["sponsorships.rubykaigi.org"]
13+
aliases = [var.app_domain]
1014

1115
viewer_certificate {
12-
acm_certificate_arn = data.aws_acm_certificate.use1-sponsorships-rk-o.arn
16+
acm_certificate_arn = var.certificate_arn
1317
cloudfront_default_certificate = false
1418
iam_certificate_id = null
1519
minimum_protocol_version = "TLSv1.2_2021"
1620
ssl_support_method = "sni-only"
1721
}
1822

19-
logging_config {
20-
bucket = "rk-aws-logs.s3.amazonaws.com"
21-
include_cookies = false
22-
prefix = "cf/sponsorships.rubykaigi.org/"
23+
dynamic "logging_config" {
24+
for_each = var.cloudfront_log_bucket != "" ? [1] : []
25+
content {
26+
bucket = var.cloudfront_log_bucket
27+
include_cookies = false
28+
prefix = var.cloudfront_log_prefix
29+
}
2330
}
2431

2532
origin {
2633
origin_id = "apprunner"
27-
domain_name = replace(aws_apprunner_service.prd.service_url, "https://", "")
34+
domain_name = replace(aws_apprunner_service.main[0].service_url, "https://", "")
2835
origin_path = null
2936
connection_attempts = 3
3037
connection_timeout = 10
3138
custom_header {
3239
name = "x-forwarded-host"
33-
value = "sponsorships.rubykaigi.org"
40+
value = var.app_domain
3441
}
3542
custom_origin_config {
3643
http_port = 80
@@ -97,7 +104,3 @@ resource "aws_cloudfront_distribution" "prd" {
97104
}
98105
}
99106
}
100-
import {
101-
to = aws_cloudfront_distribution.prd
102-
id = "E2ZBMTEBD45786"
103-
}

tf/cloudwatch.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
resource "aws_cloudwatch_log_group" "worker" {
2+
count = var.enable_shared_resources ? 1 : 0
23
name = "/ecs/sponsor-app-worker"
34
retention_in_days = 3
45
}
6+
57
resource "aws_cloudwatch_log_group" "batch" {
8+
count = var.enable_shared_resources ? 1 : 0
69
name = "/ecs/sponsor-app-batch"
710
retention_in_days = 3
811
}

0 commit comments

Comments
 (0)