DEV Community

Discussion on: Best way to make infrastructure by Terraform

Collapse
 
rolfstreefkerk profile image
Rolf Streefkerk • Edited

See here for some good guidelines:
github.com/ozbillwang/terraform-be...

In general you need to think hard on the structure of Terraform, I propose you do these 3 things;

  • use modules, keep the general resources code separate from your solution code. Later on you can even put the modules in separate git repo's and use versioning to avoid changes from breaking other solutions.
  • use a solutions/services directory, this is where you wire up all the modules together into a solution or a service. Basically your application.
  • use an environment directory, this is where you define the exact parameters of your deployment per environment (dev, test, production). There are differences and you should keep them separated at all times.

This could look like this:

- modules
  - ec2
  - vpc
  - rds
  - apigateway
- solution
  - api
  - application_a
- environment
  - dev
  - test
  - prod

You can check out my public repo for an example:

GitHub logo rpstreef / openapi-tf-example

Example of how you can use OpenAPI with AWS API Gateway, Also includes integrations with AWSLambda, AWS Cognito, AWS SNS and CloudWatch logs

OpenAPI with AWS API Gateway, Lambda, Cognito, SNS and CloudWatch logs

This repo only deploys the infrastructure via Terraform. The source code, this repo, will be deployed automatically via AWS CodePipeline. By default, it's configured to automatically deploy at every push to the master branch.

Check the companion articles series 'OpenAPI' on dev.to.

Get started

The essentials

  • Download Terraform v0.12.x here
  • You will need Node v12.x from here
  • Git, to clone this Repo, from here
  • Create a free AWS account (requires credit card) here
  • Finally, download the AWS CLI tool
  • Setup your AWS local profile, see this guide how it's done.
  • Manually setup an AWS S3 Bucket for Terraform state storage.

To get the API running

If you meet all the pre-requisites, do the following

  • In your AWS development account create the S3 bucket for your Terraform state files
    • Optionally, encrypt the S3 bucket and enable versioning…
Collapse
 
salsalyyy profile image
SalSal

Thank you for answer the questions, was really great help to make my mind clear!
If you could also know your opinion on this as well?
which one is better to create a bunch of s3 buckets for multiple environment? write a module to create them once or use simple module and apply multiple times?

Collapse
 
rolfstreefkerk profile image
Rolf Streefkerk • Edited

Do they have the same functionality across these environments (like development, test etc) then you would have 1 S3 module and in your application directory you create the single bucket. It will be named slightly different per environment (include the name environment name, dev, test,prod).

Then in your environment directory you supply the correct naming for each environment, then you have to deploy each of them separate with their respective tfstate file per environment.

That is probably the easiest way to do it and then make sure you store the Terraform state in each environment where it belongs to as well.

To target each of them, you can create profiles in your ./.aws/config file and reference them in the Terraform tfstate and your remote state configuration.

I hope that helps

Thread Thread
 
salsalyyy profile image
SalSal • Edited

Well, there are five buckets at least which will repeat in dev, stage and prod! but as far as they contain their environment name in their name, should be change per environment.