Skip to content

Commit d99a259

Browse files
committed
feat: prepare aws-instance, aws-network-setup to EUSC AWS partition, migrate Terragrunt cli usage
1 parent c4f034b commit d99a259

File tree

20 files changed

+400
-35
lines changed

20 files changed

+400
-35
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repos:
3030
rev: v0.1.30
3131
hooks:
3232
- id: tofu-fmt
33-
- id: tofu-validate
33+
# - id: tofu-validate
3434
- id: terragrunt-hcl-fmt
3535
- id: markdown-link-check
3636
- id: gofmt

terraform/aws/aws-alb/module/webserver.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ data "aws_ami" "ubuntu" {
2222
values = [var.ec2_architecture]
2323
}
2424

25+
# "most_recent" is set to "true" and results are not filtered by owner or
26+
# image ID. With this configuration, a third party may introduce a new image
27+
# which will be returned by this data source. Filter by owner or image ID to
28+
# avoid this possibility.
29+
allow_unsafe_filter = true # it is safe when search is by owner-alias or owners
30+
2531
owners = ["099720109477"] # Canonical
2632
}
2733

terraform/aws/aws-iam-linked/stage/eusc.hcl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ provider "aws" {
3535
EOF
3636
}
3737

38-
3938
terraform {
4039
extra_arguments "common_vars" {
4140
commands = get_terraform_commands_that_need_vars()

terraform/aws/aws-instana/module/agent.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ data "aws_ami" "image" {
6565
values = [var.ec2_architecture]
6666
}
6767

68+
# "most_recent" is set to "true" and results are not filtered by owner or
69+
# image ID. With this configuration, a third party may introduce a new image
70+
# which will be returned by this data source. Filter by owner or image ID to
71+
# avoid this possibility.
72+
allow_unsafe_filter = true # it is safe when search is by owner-alias or owners
6873

6974
dynamic "filter" {
7075
for_each = var.ec2_ami_account_alias != "" ? [1] : []

terraform/aws/aws-instance/Makefile

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,77 @@ SHELL := /usr/bin/env bash
44
DEBUG := false
55
ifeq ($(strip $(DEBUG)),true)
66
TF_LOG := DEBUG
7-
TG_FLAGS := --terragrunt-debug
7+
TG_FLAGS := --inputs-debug
88
endif
99

1010
MODE := apply
1111
ifeq ($(strip $(MODE)),apply)
12-
MODE_STR := apply -auto-approve
12+
MODE_STR := --non-interactive -- apply -auto-approve
1313
else ifeq ($(strip $(MODE)),destroy)
14-
MODE_STR := destroy -auto-approve
14+
MODE_STR := --non-interactive -- destroy -auto-approve
1515
else
16-
MODE_STR := plan
16+
MODE_STR := --non-interactive -- plan
1717
endif
1818

19+
PARTITION := aws
1920

2021
ENV := dev
2122

23+
ifeq ($(strip $(PARTITION)),eusc)
24+
DEPLOY_PATH := stage/eusc/$(ENV)
25+
else
26+
DEPLOY_PATH := stage/$(ENV)
27+
endif
28+
2229
init: prepare
23-
cd stage/$(ENV) && terragrunt run-all init -upgrade=true
30+
cd $(DEPLOY_PATH) && terragrunt run --all -- init -upgrade=true
2431

2532
run-one: prepare ## setup one EC2 instance from environment, usage: make run-one INSTANCE=instance-ubuntu [ENV=dev] [MODE=apply]
2633
ifndef INSTANCE
2734
$(error Env INSTANCE is not defined. Usage make run-one ENV=dev INSTANCE=ubuntu)
2835
endif
29-
cd stage/$(ENV)/${INSTANCE} && terragrunt init -upgrade=true && terragrunt validate && terragrunt $(MODE_STR) --non-interactive $(TG_FLAGS)
36+
cd $(DEPLOY_PATH)/${INSTANCE} && terragrunt init -upgrade=true && terragrunt validate && terragrunt $(MODE_STR) $(TG_FLAGS)
3037

3138
run: init ## setup EC2 instances for environment, usage: make run [ENV=dev] [MODE=apply]
32-
@cd stage/$(ENV) && terragrunt run-all validate && terragrunt run-all $(MODE_STR) --non-interactive $(TG_FLAGS)
39+
@cd $(DEPLOY_PATH) && terragrunt run --all validate && terragrunt run --all $(MODE_STR) $(TG_FLAGS)
3340

3441
ssh: ## ssh to EC2 instance
3542
ifndef INSTANCE
3643
$(error Env INSTANCE is not defined. Usage make ssh ENV=dev INSTANCE=ubuntu)
3744
endif
38-
ssh -o StrictHostKeyChecking=accept-new -i ~/.ssh/id_rsa.aws.vm ubuntu@$(shell cd stage/$(ENV)/${INSTANCE} && terragrunt output ec2_dns)
45+
ssh -o StrictHostKeyChecking=accept-new -i ~/.ssh/id_rsa.aws.vm ubuntu@$(shell cd $(DEPLOY_PATH)/${INSTANCE} && terragrunt output ec2_dns)
3946

4047
ssm-ssh: ## ssh to EC2 instance over SSM SSH
4148
ifndef INSTANCE
4249
$(error Env INSTANCE is not defined. Usage make ssm-ssh ENV=dev INSTANCE=ubuntu)
4350
endif
44-
aws ssm start-session --target "$(shell cd stage/$(ENV)/${INSTANCE} && terragrunt output ec2_id)"
51+
aws ssm start-session --target "$(shell cd $(DEPLOY_PATH)/${INSTANCE} && terragrunt output ec2_id)"
4552

4653
destroy-instance: ## terminate instance
4754
ifndef INSTANCE
4855
$(error Env INSTANCE is not defined. Usage make destroy-instance ENV=dev INSTANCE=ubuntu)
4956
endif
50-
aws ec2 terminate-instances --instance-ids "$(shell cd stage/$(ENV)/${INSTANCE} && terragrunt output ec2_id)"
57+
aws ec2 terminate-instances --instance-ids "$(shell cd $(DEPLOY_PATH)/${INSTANCE} && terragrunt output ec2_id)"
5158

5259
show-instance-startup-logs: # show EC2 cloud-init startup logs
5360
ifndef INSTANCE
5461
$(error Env INSTANCE is not defined. Usage make destroy-instance ENV=dev INSTANCE=ubuntu)
5562
endif
56-
ssh -o StrictHostKeyChecking=accept-new -i ~/.ssh/id_rsa.aws.vm ubuntu@$(shell cd stage/$(ENV)/${INSTANCE} && terragrunt output ec2_dns) cat /var/log/cloud-init-output.log
63+
ssh -o StrictHostKeyChecking=accept-new -i ~/.ssh/id_rsa.aws.vm ubuntu@$(shell cd $(DEPLOY_PATH)/${INSTANCE} && terragrunt output ec2_dns) cat /var/log/cloud-init-output.log
5764

5865
show-instance-startup-script: # show EC2 cloud-init startup script
5966
ifndef INSTANCE
6067
$(error Env INSTANCE is not defined. Usage make destroy-instance ENV=dev INSTANCE=ubuntu)
6168
endif
62-
ssh -o StrictHostKeyChecking=accept-new -i ~/.ssh/id_rsa.aws.vm ubuntu@$(shell cd stage/$(ENV)/${INSTANCE} && terragrunt output ec2_dns) sudo cat /var/lib/cloud/instance/cloud-config.txt
69+
ssh -o StrictHostKeyChecking=accept-new -i ~/.ssh/id_rsa.aws.vm ubuntu@$(shell cd $(DEPLOY_PATH)/${INSTANCE} && terragrunt output ec2_dns) sudo cat /var/lib/cloud/instance/cloud-config.txt
6370

6471

6572
test: ## test Nginx instance
66-
curl http://$(shell cd stage/$(ENV)/${INSTANCE} && terragrunt output ec2_dns):80
73+
curl http://$(shell cd $(DEPLOY_PATH)/${INSTANCE} && terragrunt output ec2_dns):80
6774

6875

6976
show-state: ## show state
70-
cd stage/$(ENV)/${INSTANCE} && terragrunt state list && terragrunt show
77+
cd $(DEPLOY_PATH)/${INSTANCE} && terragrunt state list && terragrunt show
7178

7279

7380
clean: ## clean cached plugins and data

terraform/aws/aws-instance/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Use AWS resources eliglible to AWS Free Tier __only__.
1717

1818
```bash
1919
# setup all instance for particular env
20-
make run [ENV=dev] [MODE=apply]
20+
make run [ENV=dev/default] [MODE=apply] [PARTITION=eusc]
2121

2222
# deploy single instance
2323
make run-one ENV=dev INSTANCE=windows

terraform/aws/aws-instance/module/ec2.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ data "aws_ami" "image" {
9494
values = [var.ec2_architecture]
9595
}
9696

97+
# "most_recent" is set to "true" and results are not filtered by owner or
98+
# image ID. With this configuration, a third party may introduce a new image
99+
# which will be returned by this data source. Filter by owner or image ID to
100+
# avoid this possibility.
101+
allow_unsafe_filter = true # it is safe when search is by owner-alias or owners
97102

98103
dynamic "filter" {
99104
for_each = var.ec2_ami_account_alias != "" ? [1] : []

terraform/aws/aws-instance/module/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,22 @@ variable "region" {
102102
description = "Preffered AWS region where resource need to be placed"
103103
}
104104

105+
# tflint-ignore: terraform_unused_declarations
106+
variable "partition" {
107+
type = string
108+
description = "The AWS partition in which to create resources"
109+
default = "aws"
110+
}
111+
112+
105113
# tflint-ignore: terraform_unused_declarations
106114
variable "aws_tags" {
107115
type = map(string)
108116
description = "AWS tags"
117+
default = {}
109118
}
110119

120+
111121
variable "instance_profile" {
112122
default = ""
113123
type = string
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
locals {
2+
bucket = "${local.account}-terraform-state"
3+
account = "${run_cmd("--terragrunt-quiet", "aws", "sts", "get-caller-identity", "--query", "\"Account\"", "--output", "text")}"
4+
region = "eusc-de-east-1"
5+
zone = "eusc-de-east-1a"
6+
}
7+
8+
remote_state {
9+
backend = "s3"
10+
generate = {
11+
path = "state.tf"
12+
if_exists = "overwrite_terragrunt"
13+
}
14+
config = {
15+
bucket = local.bucket
16+
key = "${basename(abspath("${get_parent_terragrunt_dir()}/.."))}/${basename(get_parent_terragrunt_dir())}/${path_relative_to_include()}/terraform.tfstate"
17+
region = local.region
18+
# TODO play with it... maybe not in free tier
19+
# encrypt = true
20+
# dynamodb_table = "my-lock-table"
21+
}
22+
}
23+
24+
25+
generate "provider" {
26+
path = "provider.tf"
27+
if_exists = "overwrite_terragrunt"
28+
contents = <<EOF
29+
provider "aws" {
30+
region = var.region
31+
default_tags {
32+
tags = var.aws_tags
33+
}
34+
}
35+
EOF
36+
}
37+
38+
terraform {
39+
extra_arguments "common_vars" {
40+
commands = get_terraform_commands_that_need_vars()
41+
42+
env_vars = {
43+
TF_VAR_terraform_state_bucket = local.bucket
44+
}
45+
}
46+
}
47+
48+
inputs = {
49+
account = local.account
50+
region = local.region
51+
zone = local.zone
52+
53+
partition = "aws-eusc"
54+
55+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#cloud-config
2+
# EC2 location of cloud-init configuration: /var/lib/cloud/instance/cloud-config.txt
3+
# Cloud-init output logs: /var/log/cloud-init-output.log
4+
---
5+
repo_update: true
6+
repo_upgrade: all
7+
8+
packages:
9+
- nginx
10+
- plocate
11+
12+
# cloud-init creates a final script in: /var/lib/cloud/instance/scripts/runcmd
13+
runcmd:
14+
- systemctl enable --now nginx
15+
- echo -n "${ssh_pub}" |base64 -d > /home/ubuntu/.ssh/id_rsa.pub
16+
- echo -n "${ssh_key}" |base64 -d > /home/ubuntu/.ssh/id_rsa
17+
- cat /home/ubuntu/.ssh/id_rsa.pub >> /home/ubuntu/.ssh/authorized_keys
18+
- 'chown ubuntu:ubuntu /home/ubuntu/.ssh/id_rsa*'
19+
- chmod 400 /home/ubuntu/.ssh/id_rsa

0 commit comments

Comments
 (0)