Skip to content

Create a new stack for shared infrastructure

This guide is outdated

We do not recommend using a shared stack like the one created in this guide any more. We recommend using micro stacks.

If you are setting up something new, please contact team Kjøremiljø first so we can do this together ❤ For more up to date information about how to set up your infrastructure you can take sneak peek at our reference app pirates-iac.

This guide can still be used for trouble shooting purposes for teams that have not yet taken the micro stack structure in use.

This stack will contain all the shared infrastructure that is needed the application stack we will create later. This includes VPC, Subnets, NAT gateways, ECS Cluster, Postgres database etc.

Prepare shared stack

Creating stacks

To create a new stack we use the ok scaffold command. This will create a new directory with the name provided as the argument to ok scaffold and generate the necessary files for Terraform to create the stack.

Navigate to the root of the environment directory:

repo-iac/dev/remote_state
cd ..

Run the ok scaffold command to create a new stack.

Naming stacks

You could name the stack anything you want, but we recommend that you use infra for the shared infrastructure resources.

repo-iac/dev
ok scaffold infra

Verify that the stack was created:

repo-iac/dev
cd infra/
ls -la

The scaffold command created two new files:

  • config.tf for configuring Terraform local variables.
  • common.tf with basic Terraform setup (the AWS provider, the S3 backend we set up above and some shared configuration variables used by the Golden Path modules).

We can now proceed to add the configuration we want to use in this stack.

Add modules to shared stack

Replacing variables

For each of the templates you're about to download, you will need to open each one of the files and replace some variables with your own values. The variables that you need to replace are documented in each of the files.

Make sure you are in the infra directory.

VPC configuration:

repo-iac/dev/infra
ok get-template vpc
ok get-template _variables_vpc.tf
ok get-template _config_vpc.auto.tfvars.json

Root Domain configuration:

repo-iac/dev/infra
ok get-template root_domain

ECS Cluster configuration:

repo-iac/dev/infra
ok get-template ecs_cluster

RDS Aurora PostgreSQL Serverless configuration:

repo-iac/dev/infra
ok get-template postgres_aurora_serverless

Apply configuration to set up the stack

repo-iac/dev/infra
terraform init
terraform apply

Coffee break

This step might take anywhere from 10 to 15 minutes to complete, and once complete it will display some important output that you will need to use in the next step.

Review output from stack creation

Adding DNS Name Servers

Onceterraform applycompletes, a list of name servers will be printed to console.

Output from terraform apply
name_servers = tolist([
  "ns-123.awsdns-44.org",
  "ns-321.awsdns-29.co.uk",
  "ns-213.awsdns-35.com",
  "ns-231.awsdns-18.net",
])

These are the name servers for the root domain specified in the root_domain.tf module. Because oslo.systems was specified as the root domain, follow this guide Adding a subdomain to oslo.systems to complete the setup.

The domain you defined (my-team-dev.oslo.systems) should now resolve to the application or applications you deploy in the next step of the guide.

To verify that the subdomain is properly registered with a name server:

dig NS +short {your-subdomain}.oslo.systems

This should give you a list of name servers corresponding to the list of name servers printed after the last terraform apply above.

If you don't have dig installed, use this DNS lookup tool to check {your-subdomain}.oslo.systems.