Skip to content

Commit 0b0c8de

Browse files
committed
refactor: Change the label
1 parent f03796d commit 0b0c8de

File tree

4 files changed

+243
-227
lines changed

4 files changed

+243
-227
lines changed

aws.tf

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
provider "aws" {
2+
region = var.cloud_provider_region
3+
}
4+
5+
# Conditional creation of data bucket
6+
module "automq_byoc_data_bucket_name" {
7+
source = "terraform-aws-modules/s3-bucket/aws"
8+
version = "4.1.2"
9+
10+
create_bucket = var.automq_byoc_data_bucket_name == "" ? true : false
11+
bucket = "automq-data-${var.automq_byoc_env_id}"
12+
force_destroy = true
13+
}
14+
15+
# Conditional creation of ops bucket
16+
module "automq_byoc_ops_bucket_name" {
17+
source = "terraform-aws-modules/s3-bucket/aws"
18+
version = "4.1.2"
19+
20+
create_bucket = var.automq_byoc_ops_bucket_name == "" ? true : false
21+
bucket = "automq-ops-${var.automq_byoc_env_id}"
22+
force_destroy = true
23+
}
24+
25+
data "aws_availability_zones" "available" {}
26+
27+
module "automq_byoc_vpc" {
28+
source = "terraform-aws-modules/vpc/aws"
29+
version = "5.0.0"
30+
31+
count = var.create_new_vpc ? 1 : 0
32+
33+
name = "automq-byoc-vpc-${var.automq_byoc_env_id}"
34+
cidr = "10.0.0.0/16"
35+
36+
azs = slice(data.aws_availability_zones.available.names, 0, 3)
37+
public_subnets = ["10.0.0.0/20"]
38+
private_subnets = ["10.0.128.0/20", "10.0.144.0/20", "10.0.160.0/20"]
39+
40+
enable_dns_support = true
41+
enable_dns_hostnames = true
42+
43+
tags = {
44+
45+
automqVendor = "automq"
46+
automqEnvironmentID = var.automq_byoc_env_id
47+
}
48+
}
49+
50+
resource "aws_security_group" "endpoint_sg" {
51+
count = var.create_new_vpc ? 1 : 0
52+
53+
name = "automq-byoc-endpoint-sg-${var.automq_byoc_env_id}"
54+
description = "Security group for VPC endpoint"
55+
vpc_id = module.automq_byoc_vpc[0].vpc_id
56+
57+
ingress {
58+
from_port = 443
59+
to_port = 443
60+
protocol = "tcp"
61+
cidr_blocks = ["0.0.0.0/0"]
62+
}
63+
64+
egress {
65+
from_port = 0
66+
to_port = 0
67+
protocol = "-1"
68+
cidr_blocks = ["0.0.0.0/0"]
69+
}
70+
71+
tags = {
72+
Name = "automq-byoc-endpoint-sg-${var.automq_byoc_env_id}"
73+
automqVendor = "automq"
74+
automqEnvironmentID = var.automq_byoc_env_id
75+
}
76+
}
77+
78+
resource "aws_vpc_endpoint" "ec2" {
79+
count = var.create_new_vpc ? 1 : 0
80+
81+
vpc_id = module.automq_byoc_vpc[0].vpc_id
82+
service_name = "com.amazonaws.${var.cloud_provider_region}.ec2"
83+
vpc_endpoint_type = "Interface"
84+
security_group_ids = [aws_security_group.endpoint_sg[0].id]
85+
subnet_ids = module.automq_byoc_vpc[0].private_subnets
86+
87+
private_dns_enabled = true
88+
89+
tags = {
90+
Name = "automq-byoc-ec2-endpoint-${var.automq_byoc_env_id}"
91+
automqVendor = "automq"
92+
automqEnvironmentID = var.automq_byoc_env_id
93+
}
94+
}
95+
96+
resource "aws_vpc_endpoint" "s3" {
97+
count = var.create_new_vpc ? 1 : 0
98+
99+
vpc_id = module.automq_byoc_vpc[0].vpc_id
100+
service_name = "com.amazonaws.${var.cloud_provider_region}.s3"
101+
vpc_endpoint_type = "Gateway"
102+
103+
route_table_ids = concat(
104+
module.automq_byoc_vpc[0].public_route_table_ids,
105+
module.automq_byoc_vpc[0].private_route_table_ids
106+
)
107+
108+
tags = {
109+
Name = "automq-byoc-s3-endpoint-${var.automq_byoc_env_id}"
110+
automqVendor = "automq"
111+
automqEnvironmentID = var.automq_byoc_env_id
112+
}
113+
}
114+
115+
locals {
116+
automq_byoc_vpc_id = var.create_new_vpc ? module.automq_byoc_vpc[0].vpc_id : var.automq_byoc_vpc_id
117+
automq_byoc_env_console_public_subnet_id = var.create_new_vpc ? element(module.automq_byoc_vpc[0].public_subnets, 0) : var.automq_byoc_env_console_public_subnet_id
118+
automq_data_bucket = var.automq_byoc_data_bucket_name == "" ? module.automq_byoc_data_bucket_name.s3_bucket_id : "${var.automq_byoc_data_bucket_name}-${var.automq_byoc_env_id}"
119+
automq_ops_bucket = var.automq_byoc_ops_bucket_name == "" ? module.automq_byoc_ops_bucket_name.s3_bucket_id : "${var.automq_byoc_ops_bucket_name}-${var.automq_byoc_env_id}"
120+
}
121+
122+
data "aws_vpc" "selected" {
123+
id = local.automq_byoc_vpc_id
124+
}
125+
126+
locals {
127+
ssm_parameter_path = "/aws/service/marketplace/prod-nl2cyzygb46fw/${var.automq_byoc_env_version}"
128+
}
129+
130+
data "aws_ssm_parameter" "marketplace_ami" {
131+
name = local.ssm_parameter_path
132+
}
133+
134+
data "aws_ami" "marketplace_ami_details" {
135+
most_recent = true
136+
137+
filter {
138+
name = "image-id"
139+
values = [data.aws_ssm_parameter.marketplace_ami.value]
140+
}
141+
}
142+
143+
resource "aws_security_group" "allow_all" {
144+
vpc_id = data.aws_vpc.selected.id
145+
146+
ingress {
147+
from_port = 8080
148+
to_port = 8080
149+
protocol = "tcp"
150+
cidr_blocks = [var.automq_byoc_env_console_cidr]
151+
}
152+
153+
egress {
154+
from_port = 0
155+
to_port = 0
156+
protocol = "-1"
157+
cidr_blocks = ["0.0.0.0/0"]
158+
}
159+
}
160+
161+
resource "aws_iam_role" "automq_byoc_role" {
162+
name = "automq-byoc-service-role-${var.automq_byoc_env_id}"
163+
164+
assume_role_policy = jsonencode({
165+
Version = "2012-10-17"
166+
Statement = [
167+
{
168+
Action = "sts:AssumeRole"
169+
Effect = "Allow"
170+
Sid = ""
171+
Principal = {
172+
Service = "ec2.amazonaws.com"
173+
}
174+
},
175+
]
176+
})
177+
}
178+
179+
resource "aws_iam_policy" "automq_byoc_policy" {
180+
name = "automq-byoc-service-policy-${var.automq_byoc_env_id}"
181+
description = "Custom policy for automq_byoc service"
182+
183+
policy = templatefile("${path.module}/tpls/automq_byoc_role_policy.json.tpl", {
184+
automq_data_bucket = local.automq_data_bucket
185+
automq_ops_bucket = local.automq_ops_bucket
186+
})
187+
}
188+
189+
resource "aws_iam_role_policy_attachment" "automq_byoc_role_attachment" {
190+
role = aws_iam_role.automq_byoc_role.name
191+
policy_arn = aws_iam_policy.automq_byoc_policy.arn
192+
}
193+
194+
resource "aws_iam_instance_profile" "automq_byoc_instance_profile" {
195+
name = "automq-byoc-instance-profile-${var.automq_byoc_env_id}"
196+
role = aws_iam_role.automq_byoc_role.name
197+
}
198+
199+
resource "aws_route53_zone" "private" {
200+
name = "${var.automq_byoc_env_id}.automq.private"
201+
202+
vpc {
203+
vpc_id = local.automq_byoc_vpc_id
204+
}
205+
206+
lifecycle {
207+
create_before_destroy = true
208+
}
209+
}
210+
211+
locals {
212+
aws_iam_instance_profile_arn_encoded = urlencode(aws_iam_instance_profile.automq_byoc_instance_profile.arn)
213+
}
214+
215+
resource "aws_eip" "web_ip" {
216+
instance = aws_instance.web.id
217+
}

0 commit comments

Comments
 (0)