Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Continuous Infrastructure Delivery

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →
Avatar for Xander Xander
February 11, 2020

Continuous Infrastructure Delivery

Avatar for Xander

Xander

February 11, 2020

Other Decks in Programming

Transcript

  1. CODE EDITOR # configure the aws provider provider "aws" {

    profile = "default" region = "us-east-1" } # create an instance resource "aws_instance" "example" { ami = “ami-2757f631" instance_type = "t2.micro" }
  2. CODE EDITOR func TestTerraformAwsExample(t *testing.T) { t.Parallel() // Give this

    EC2 Instance a unique ID expectedName := fmt.Sprintf("terratest-aws-example-%s", random.UniqueId()) // At the end of the test, run `terraform destroy` to clean up any resources that were created defer terraform.Destroy(t, terraformOptions) // This will run `terraform init` and `terraform apply` and fail the test if there are any errors terraform.InitAndApply(t, terraformOptions) aws.AddTagsToResource(t, awsRegion, instanceID, map[string]string{"testing": "testing-tag-value"}) // Look up the tags for the given Instance ID instanceTags := aws.GetTagsForEc2Instance(t, awsRegion, instanceID) testingTag, containsTestingTag := instanceTags["testing"] assert.True(t, containsTestingTag) }
  3. CODE EDITOR # call the servers module with a value

    of 5 servers module "servers" { source = "./app-cluster" servers = 5 }
  4. CODE EDITOR # feature flag variable to enable new ami

    variable "enable_new_ami" { default = false type = bool } # aws instance enabled by feature flag resource "aws_instance" "example_bionic" { count = var.enable_new_ami ? 1 : 0 instance_type = "t2.micro" ami = data.aws_ami.ubuntu_bionic.id vpc_security_group_ids = [aws_security_group.instances.id] subnet_id = aws_subnet.public.id tags = { has_toggle = var.enable_new_ami } }
  5. CODE EDITOR version: 2 jobs: test: docker: - image: salaxander/terratest:latest

    steps: - checkout - run : | go test -v -timeout 30m workflows: version: 2 ci: jobs: - test filters: branches: only: master
  6. CODE EDITOR version: 2 jobs: plan: docker: - image: hashicorp/terraform:0.12.20

    steps: - checkout - run: | terraform init && terraform plan apply: docker: - image: hashicorp/terraform:0.12.20 steps: - checkout - run: | terraform init && terraform apply --auto-approve workflows: version: 2 ci: jobs: - plan - approve: type: approval requires: - plan - apply: requires: - approve filters: branches: only: master