How to deploy and redeploy applications with Terraform? How to deploy and redeploy applications with Terraform? jenkins jenkins

How to deploy and redeploy applications with Terraform?


For deploy/redeploy, you can use another solution by Hashicorp: Nomad. It uses the same language as Terraform to program tasks that you can run on a cluster. Tasks can be anything, for example: redeploy all my web app instances.


I'm using CodeDeploy with Terraform/Chef. The setup I'm using goes something like this:

1) Manually setup the CodeDeploy IAM Roles ahead of time.

2) Setup the CodeDeploy App/Group ahead of time.

3) Setup the Instance Profile using Terraform, like this:

resource "aws_iam_instance_profile" "code_deploy" {    name = "CodeDeploy"    roles = ["${var.codedeploy_instance_role}"]}

4) Use the Instance Profile and the correct tags (that match your CodeDeploy app) when making an instance, like this:

iam_instance_profile = "${aws_iam_instance_profile.code_deploy.id}"tags {   CD = "${var.tag_cd}"}

5) Use Chef (or whatever your provisioner is) to setup CodeDeploy on the instance.

Then you're good to use CodeDeploy like normal.


Adding this so that in case someone is looking for more information, might find this useful.

Building on the Solution from Peter, I am setting up the CodeDeploy IAM Roles and CodeDeploy App/Group from Terraform as well. Here is what I have:

resource "aws_iam_role" "codedeploy_role_name" {  name = "codedeploy_role_name"  assume_role_policy = <<EOF{    "Version": "2012-10-17",    "Statement": [      {        "Effect": "Allow",        "Principal": {          "Service": [            "codedeploy.amazonaws.com",            "ec2.amazonaws.com"          ]        },        "Action": "sts:AssumeRole"      }    ]}EOF}resource "aws_codedeploy_app" "analytics_app" {  name = "analytics_app"}resource "aws_codedeploy_deployment_config" "analytics_deployment_config" {  deployment_config_name = "analytics_deployment_config"  minimum_healthy_hosts {    type  = "HOST_COUNT"    value = 2  }}resource "aws_codedeploy_deployment_group" "analytics_group" {  app_name              = "${aws_codedeploy_app.analytics_app.name}"  deployment_group_name = "analytics_group"  service_role_arn      = "${aws_iam_role.codedeploy_role_name.arn}"  deployment_config_name = "analytics_deployment_config"  ec2_tag_filter {    key   = "CodeDeploy"    type  = "KEY_AND_VALUE"    value = "analytics"  }  auto_rollback_configuration {    enabled = true    events  = ["DEPLOYMENT_FAILURE"]  }}