Skip to content

Commit 897d044

Browse files
authored
Incremental PR to get VPC and remote state import (#32)
* Creating base VPC. Added stub for base Service and imported remote VPC state * Export entire VPC definition
1 parent 8b3b3e9 commit 897d044

File tree

11 files changed

+265
-0
lines changed

11 files changed

+265
-0
lines changed

devops-tooling/envs.makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export STACK_ENV=local
4343
export CDK_CMD=cdklocal
4444
export TFORM_CMD=tflocal
4545
export DOCKER_DEFAULT_PLATFORM=linux/arm64
46+
export IAC_DDB_TABLE=terraform_locks
4647

4748
# Pattern specific variables for each pipeline
4849
# Global local pipeline vars

devops-tooling/tf-basesvc.makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Define the target specific environment variables needed
2+
# for the local-tf-vpcbase targets.
3+
#
4+
local-tf-basesvc%: export IAC_DIR=iac/terraform/hcl/basesvc
5+
local-tf-basesvc%: export APP_NAME=basesvc
6+
local-tf-basesvc%: export CF_BUCKET_NAME=cfbucket
7+
8+
9+
# Initialize the terraform stack
10+
local-tf-basesvc-init: tf-stack-init
11+
echo 'bucket_name="$(CF_BUCKET_NAME)"' >> $(IAC_DIR)/$(STACK_SUFFIX).auto.tfvars
12+
13+
# Plan the terraform stack
14+
local-tf-basesvc-plan: tf-stack-plan
15+
16+
# Apply the terraform stack
17+
local-tf-basesvc-apply: tf-stack-apply
18+
19+
local-tf-basesvc-output:
20+
@$(MAKE) --silent tf-stack-output > $(IAC_DIR)/terraform_output.json

devops-tooling/tf-basevpc.makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Define the target specific environment variables needed
2+
# for the local-tf-vpcbase targets.
3+
#
4+
local-tf-basevpc%: export IAC_DIR=iac/terraform/hcl/basevpc
5+
local-tf-basevpc%: export APP_NAME=basevpc
6+
local-tf-basevpc%: export CF_BUCKET_NAME=cfbucket
7+
8+
9+
# Initialize the terraform stack
10+
local-tf-basevpc-init: tf-stack-init
11+
echo 'bucket_name="$(CF_BUCKET_NAME)"' >> $(IAC_DIR)/$(STACK_SUFFIX).auto.tfvars
12+
13+
# Plan the terraform stack
14+
local-tf-basevpc-plan: tf-stack-plan
15+
16+
# Apply the terraform stack
17+
local-tf-basevpc-apply: tf-stack-apply
18+
19+
local-tf-basevpc-output:
20+
@$(MAKE) --silent tf-stack-output > $(IAC_DIR)/terraform_output.json
21+
22+
#local-tf-basevpc-test:
23+
# cd auto_tests/jest && npm install && npx jest

devops-tooling/tf.makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local-tf-create-iac-bucket:
22
$(AWS_CMD) s3api create-bucket --region $(AWS_REGION) --bucket $(IAC_BUCKET)
33
$(AWS_CMD) s3api put-bucket-versioning --bucket $(IAC_BUCKET) --versioning-configuration Status=Enabled
4+
$(AWS_CMD) dynamodb create-table --table-name $(IAC_DDB_TABLE) --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5
5+
46

57
make-tf-vars:
68
@rm -f $(IAC_DIR)/*.auto.tfvars
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
terraform {
2+
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
}
7+
null = {
8+
source = "hashicorp/null"
9+
}
10+
}
11+
12+
backend "s3" {
13+
encrypt = true
14+
dynamodb_table = "terraform_locks"
15+
}
16+
}

iac/terraform/hcl/basesvc/main.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
provider "aws" {
2+
region = var.aws_region
3+
skip_credentials_validation = var.localstack ? true : false
4+
skip_requesting_account_id = var.localstack ? true : false
5+
}
6+
7+
8+
data "terraform_remote_state" "vpc" {
9+
backend = "s3"
10+
11+
config = {
12+
encrypt = true
13+
bucket = var.tfstate_bucket_name
14+
key = "basevpc/${var.stack_env}/terraform.tfstate"
15+
region = var.aws_region
16+
dynamodb_table = "terraform_locks"
17+
skip_credentials_validation = var.localstack ? true : false
18+
skip_requesting_account_id = var.localstack ? true : false
19+
20+
access_key = var.localstack ? "test" : null
21+
secret_key = var.localstack ? "test" : null
22+
endpoints = var.localstack ? {
23+
s3 = "http://s3.localhost.localstack.cloud:4566"
24+
dynamo_db = "http://localhost:4566"
25+
iam = "http://localhost:4566"
26+
sts = "http://localhost:4566"
27+
} : null
28+
}
29+
}
30+
31+
#
32+
33+
output "vpc_name" {
34+
value = data.terraform_remote_state.vpc.outputs.base_vpc.name
35+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Please change the default names as per your requirements.
2+
3+
variable "aws_profile" {
4+
description = "AWS profile name"
5+
type = string
6+
}
7+
8+
variable "aws_region" {
9+
description = "AWS region"
10+
type = string
11+
}
12+
13+
variable "localstack" {
14+
description = "LocalStack deploy"
15+
type = bool
16+
}
17+
18+
variable "cidr_block" {
19+
default = "10.100.0.0/16"
20+
type = string
21+
}
22+
23+
variable "app_name" {
24+
default = "myapp"
25+
type = string
26+
}
27+
28+
variable "stack_env" {
29+
default = "dev"
30+
type = string
31+
}
32+
33+
variable "logging_level" {
34+
default = "debug"
35+
type = string
36+
}
37+
38+
variable "bucket_name" {
39+
description = "CloudFront S3 Origin bucket name"
40+
type = string
41+
}
42+
43+
variable "tfstate_bucket_name" {
44+
default = "terraform-state"
45+
type = string
46+
}
47+
48+
variable "created_by" {
49+
default = "LocalStack"
50+
type = string
51+
}
52+
53+
variable "object_ownership" {
54+
default = "BucketOwnerPreferred"
55+
type = string
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
terraform {
2+
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
}
7+
null = {
8+
source = "hashicorp/null"
9+
}
10+
}
11+
12+
backend "s3" {
13+
encrypt = true
14+
dynamodb_table = "terraform_locks"
15+
}
16+
}

iac/terraform/hcl/basevpc/main.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
provider "aws" {
2+
region = var.aws_region
3+
skip_credentials_validation = var.localstack ? true : false
4+
skip_requesting_account_id = var.localstack ? true : false
5+
}
6+
7+
8+
# Create a VPC in 3 AZs with public and private subnets, 1 NAT Gateway, and 1 Internet Gateway
9+
module "vpc" {
10+
source = "terraform-aws-modules/vpc/aws"
11+
12+
name = "basevpc"
13+
cidr = "10.100.0.0/16"
14+
15+
azs = ["us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d"]
16+
private_subnets = ["10.100.1.0/24", "10.100.2.0/24", "10.100.3.0/24", "10.100.4.0/24"]
17+
public_subnets = ["10.100.101.0/24", "10.100.102.0/24", "10.100.103.0/24", "10.100.104.0/24"]
18+
19+
enable_nat_gateway = true
20+
single_nat_gateway = true
21+
enable_vpn_gateway = false
22+
23+
tags = {
24+
Terraform = "true"
25+
Environment = "dev"
26+
Name = "basevpc"
27+
}
28+
}
29+
#
30+
output "base_vpc" {
31+
value = module.vpc
32+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Please change the default names as per your requirements.
2+
3+
variable "aws_profile" {
4+
description = "AWS profile name"
5+
type = string
6+
}
7+
8+
variable "aws_account_id" {
9+
description = "AWS Account ID"
10+
default = "000000000000"
11+
type = string
12+
}
13+
14+
variable "aws_region" {
15+
description = "AWS region"
16+
type = string
17+
}
18+
19+
variable "localstack" {
20+
description = "LocalStack deploy"
21+
type = bool
22+
}
23+
24+
variable "cidr_block" {
25+
default = "10.100.0.0/16"
26+
type = string
27+
}
28+
29+
variable "app_name" {
30+
default = "myapp"
31+
type = string
32+
}
33+
34+
variable "stack_env" {
35+
default = "dev"
36+
type = string
37+
}
38+
39+
variable "logging_level" {
40+
default = "debug"
41+
type = string
42+
}
43+
44+
variable "bucket_name" {
45+
description = "CloudFront S3 Origin bucket name"
46+
type = string
47+
}
48+
49+
variable "tfstate_bucket_name" {
50+
default = "terraform-state"
51+
type = string
52+
}
53+
54+
variable "created_by" {
55+
default = "LocalStack"
56+
type = string
57+
}
58+
59+
variable "object_ownership" {
60+
default = "BucketOwnerPreferred"
61+
type = string
62+
}

0 commit comments

Comments
 (0)