Skip to content

Latest commit

 

History

History
138 lines (120 loc) · 4.81 KB

README.md

File metadata and controls

138 lines (120 loc) · 4.81 KB

i18n

日本語版はこちらになります

What's Terraform

Terraform is an IaC(infrastructure as code) software tool, which created by HashiCorp.
Listed as a recommended tool for Infrastructure Provisioning at DevOps Roadmap,
Terraform is a perfect tool for those engaged in occupations such as IT infrastructure engineer, DevOps engineer, and SRE.

Getting Started

  • Based on the content for help junior SRE onboarding used in-house. -> Drecom
  • A practice repository for using terraform via hands-on to show you how terraform working and what's Drecom-style.
  • Please note that the Drecom-style terraform usage patterns are included, these may not be best practice but we use them for our daily work.
  • AWS Resources used:
    • IAM
    • S3
    • Api-gateway
    • Lambda function
    • ECR
    • App Runner

Who might be interested

  • Terraform beginners
  • Junior DevOps engineers
  • Junior SREs
  • etc.

Prerequisite

  • Terminal application(e.g. iTerm2, wsl2)
  • curl
  • AWS account(with access key & secret access key)
  • A modern browser
  • Terraform(0.12+)
    • Used Ver. 1.0.0 here
  • docker

Usage

  • Set your AWS Credentials into a example file
    • service_account_id = ""
    • aws_access_key = ""
    • aws_secret_key = ""
    • region = "ap-northeast-1"
$ mv terraform.tfvars.example terraform.tfvars
$ vi terraform.tfvars
  • To prevent the resources of each environment (staging, production, etc.) from being influenced by each other, Drecom use terraform workspace to get it done.
  • Firstly, make a workspace called 'common' to build IAM, S3 bucket resources to your AWS env, via terraform plan & apply
  • Secondly, make a workspace called 'system' to build lambda-function, api-gateway, ecr resources to your AWS env, via terraform plan & apply
  • Finally, make a workspace called 'production' to build apprunner resources to your AWS env, via terraform plan & apply
    • Be free to add other .tf files to see how to build others resources.
    • Discussion with your partner based on issues, and see what you can do togerther.
  • Don't forget to clear up all AWS resources after workshop, or you may recieve a surprise bill from AWS. :)

Workshop

You may found the tfstate files are stored here in the local environment,
but the truth is we do all that stuff via CI tool(gitlab-ci, in our case),
since it has some sensitive information that not suitable to be published, so let's do it locally here.

1)Build common resources

$ git clone [email protected]:drecom/terraform-onboarding.git
$ cd terraform-onboarding
$ mv terraform.tfvars.example terraform.tfvars
$ vi terraform.tfvars
# Several variables are defined by variables.tf   
# Before running other command, you may take a look at it.  
$ vi variables.tf
$ terraform init
# show all existing workspaces
$ terraform workspace list
$ terraform workspace select common
$ terraform plan
$ terraform apply

2)Build system resources

2-1)Upload lambda function to S3 bucket

See drecom/demo-box's README for more details.

2-2)Build resources

$ terraform workspace select system
$ terraform plan
$ terraform apply

2-3)Say hello to lambda function

# on workspace system
$ terraform output | grep deployment-invoke-url | awk '{print $3}' | xargs curl
{"drecom": "with entertainment <TIME_NOW+0900>"}

3)Build production resources

3-1) Push application image to AWS ECR

See sample-app's README for more details.

3-2) Build resources

$ terraform workspace select production
$ terraform plan
# It usually takes several minutes
$ terraform apply

3-3) Access the output's URL via browser

  • apprunner-url

3-option) Create an EC2 instance and install nginx

$ terraform workspace select production
# switch “production = false” to “production = true”
$ vi ec2_variables.tf
$ terraform plan
# It usually takes several minutes
$ terraform apply
# until instance healthy(about 3min)
# check it via AWS console
$ terraform output | grep practice-ec2-public-dns | awk '{print $3}' | xargs curl

Don't forget to clear up resources after workshop

$ terraform workspace select production
$ terraform destroy
$ terraform workspace select system
$ terraform destroy
$ terraform workspace select common
$ terraform destroy

Tips

  • We use some runtime manager to help us handling different versions of terraform. (e.g.