Skip to content

Instantly share code, notes, and snippets.

@joekhoobyar
Last active August 29, 2015 14:23
Show Gist options
  • Save joekhoobyar/5421b3ebbf9c313404ec to your computer and use it in GitHub Desktop.
Save joekhoobyar/5421b3ebbf9c313404ec to your computer and use it in GitHub Desktop.
Terraform #2493 - Cannot delete due to cycles when using create_before_destroy
provider "aws" {
region = "${var.aws_region}"
}
resource "terraform_remote_state" "mobilecloud" {
backend = "s3"
config {
bucket = "devops-terraform-state"
key = "mobilecloud-deploy-${var.environment}"
region = "${var.aws_region}"
}
}
# Mobile cloud IAM role needed to download deployment artifacts from S3.
resource "aws_iam_role" "mobilecloud" {
name = "mobilecloud-${var.environment}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "mobilecloud" {
name = "mobilecloud-${var.environment}-deploy_from_s3"
role = "${aws_iam_role.mobilecloud.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets"
],
"Resource": [
"arn:aws:s3:::*"
]
},
{
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::ellucian-mobile"
]
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:HeadObject"
],
"Resource": [
"arn:aws:s3:::ellucian-mobile/*"
]
}
]
}
EOF
}
resource "aws_iam_instance_profile" "mobilecloud" {
name = "mobilecloud-${var.environment}"
roles = ["${aws_iam_role.mobilecloud.name}"]
}
# Mobile cloud bootstrapping script.
resource "template_file" "mobilecloud-bootstrap" {
filename = "templates/bootstrap.sh"
vars {
playbook_s3_url = "${var.mobilecloud_playbook_url}"
war_build_number = "${var.mobilecloud_war_build_number}"
deploy_environment = "${var.environment}"
}
lifecycle {
create_before_destroy = true
}
}
# Mobile cloud security group
resource "aws_security_group" "mobilecloud" {
name = "mobilecloud-${var.environment}"
description = "Enables access to mobilecloud server(s)"
vpc_id = "${var.aws_vpc_id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Mobile cloud ASG and LC
resource "aws_launch_configuration" "mobilecloud" {
# name = "mobilecloud-${var.environment}-LC"
image_id = "${var.ami_id}"
instance_type = "t2.small"
iam_instance_profile = "${aws_iam_instance_profile.mobilecloud.name}"
associate_public_ip_address = true
key_name = "${var.aws_key_name}"
security_groups = ["${aws_security_group.mobilecloud.id}"]
user_data = "${template_file.mobilecloud-bootstrap.rendered}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "mobilecloud" {
availability_zones = ["us-east-1a", "us-east-1e","us-east-1d","us-east-1c"]
vpc_zone_identifier = ["subnet-33402009", "subnet-846e62c2", "subnet-a329cad4", "subnet-c7301cef"]
name = "mobilecloud-${var.environment}-ASG"
min_size = 2
max_size = 2
desired_capacity = 2
launch_configuration = "${aws_launch_configuration.mobilecloud.id}"
load_balancers = ["mobileCloud-${var.environment}"]
force_delete = true
tag {
key = "Name"
value = "mobilecloud-${var.environment}"
propagate_at_launch = true
}
tag {
key = "service"
value = "mobilecloud"
propagate_at_launch = true
}
tag {
key = "environment"
value = "${var.environment}"
propagate_at_launch = true
}
tag {
key = "buildNumber"
value = "${var.build_number}"
propagate_at_launch = true
}
lifecycle {
create_before_destroy = true
}
}
$ terraform destroy -var service="mobilecloud" -var environment="dev" -var-file=mobilecloud_war.tfvars.json -var-file=mobilecloud_playbook.tfvars.json -var-file=terraform.tfvars.json
Do you really want to destroy?
Terraform will delete all your managed infrastructure.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
template_file.mobilecloud-bootstrap: Refreshing state... (ID: 3d37ebd1a409a466f322a50e6f10a05aa3159ea16c41d33a6dc624731c2b9320)
terraform_remote_state.mobilecloud: Refreshing state... (ID: 2015-06-24 02:12:17.056841 +0000 UTC)
aws_security_group.mobilecloud: Refreshing state... (ID: sg-6134d006)
aws_iam_role.mobilecloud: Refreshing state... (ID: mobilecloud-dev)
aws_iam_role_policy.mobilecloud: Refreshing state... (ID: mobilecloud-dev:mobilecloud-dev-deploy_from_s3)
aws_iam_instance_profile.mobilecloud: Refreshing state... (ID: mobilecloud-dev)
aws_launch_configuration.mobilecloud: Refreshing state... (ID: terraform-y3m7r6pstbgxbdira65deppxgi)
aws_autoscaling_group.mobilecloud: Refreshing state... (ID: mobilecloud-dev-ASG)
Error creating plan: 1 error(s) occurred:
* Cycle: aws_autoscaling_group.mobilecloud (destroy), provider.aws (close), aws_security_group.mobilecloud (destroy), aws_security_group.mobilecloud, aws_iam_instance_profile.mobilecloud, aws_launch_configuration.mobilecloud, aws_autoscaling_group.mobilecloud, aws_launch_configuration.mobilecloud (destroy), aws_iam_instance_profile.mobilecloud (destroy), aws_iam_role.mobilecloud (destroy), aws_iam_role.mobilecloud, aws_iam_role_policy.mobilecloud
# AWS Variables
variable "aws_region" {
default = "us-east-1"
}
# AMI that is used to launch instances.
variable "ami_id" {}
# VPC used to contain infrastructure
variable "aws_vpc_id" {
default = "vpc-5961ae3c"
}
# Key name of the key-pair that is used to launch instances.
variable "aws_key_name" {
default = "mobileKeyPair"
}
# Load balancers that instances are launched behind.
variable "load_balancer" {
default = {
dev = "mobileCloud-dev"
staging = "mobileCloud-staging"
production = "mobileCloud-production"
}
}
# CI/CD Variables
variable "environment" {
default = "dev"
}
variable "build_number" {
default = "Unspecified"
}
variable "mobilecloud_playbook_url" {
default = "s3://ellucian-mobile/jobs/ansible-playbook-mobilecloud-deploy/9/ansible-playbook-mobilecloud-deploy-0.2-full.zip"
}
variable "mobilecloud_war_build_number" {
default = "23"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment