Terraform v0.11.2
resource "aws_batch_compute_environment" "env1" {
compute_environment_name = "env1"
compute_resources {
instance_role = "arn:aws:iam::XXXXXXXXXXXX:instance-profile/aws-batch-instance-role-policy"
instance_type = [
"optimal",
]
max_vcpus = 16
min_vcpus = 0
desired_vcpus = 2
subnets = [
"${aws_subnet.main.id}",
]
type = "EC2"
image_id = "${data.aws_ami.ecs_optimized.image_id}"
security_group_ids = [
"${aws_default_security_group.default.id}",
]
ec2_key_pair = "my-key"
}
service_role = "arn:aws:iam::XXXXXXXXXXXX:role/service-role/aws-batch-service-role"
type = "MANAGED"
lifecycle {
create_before_destroy = true
}
}
data "aws_ami" "ecs_optimized" {
most_recent = true
owners = ["amazon"]
filter = {
name = "name"
values = ["amzn-ami-*.*.*-amazon-ecs-optimized"]
}
}
I'm using ECS-optimized AMI provided by Amazon, which is sometimes updated.
Then I defined data.aws_ami.ecs_optmized
for track the AMI update automatically.
I expect terraform to create new compute environment with the new AMI with new environment name, and then delete the existing compute environment with the old AMI.
Terraform fails to create new compute environment with the following error, because it tries to create a new environment with the same name.
* aws_batch_compute_environment.env1: 1 error(s) occurred:
* aws_batch_compute_environment.env1: : Object already exists
status code: 409, request id: edc9b72d-0645-11e8-a353-cf4b5406a3a1
I expect HCL can define compute environments' prefix, like name_prefix
in aws_launch_configuration
.
With such feature we can switch to a new compute environment by automating create (new environment), update (job queue), and delete (old environment) to update AMI.
terraform apply
Here is a "fix" I have been using to simulate the name_prefix functionality. You can use the random resource to create stateful randomness. When you want to create a new compute environment you can just fiddle the random_string length which will cleanly create the new resources then delete the old resource.
resource "random_string" "name" {
length = 16
special = false
upper = false
}
resource "aws_batch_compute_environment" "this" {
compute_environment_name = "${var.name}-${random_string.name.result}"
# removed for brevity
lifecycle {
create_before_destroy = true
}
}
Going to try and make a pull request to add a compute_environment_name_prefix
field. My apologies if I just missed it, but is there a slack org or some sort of forum for this plugin? Was having some trouble running a subset of the tests and figured asking for help would be faster than digging in too far.
My apologies if I just missed it, but is there a slack org or some sort of forum for this plugin? Was having some trouble running a subset of the tests and figured asking for help would be faster than digging in too far.
Not specific to the terraform provider but have you tried https://gitter.im/hashicorp-terraform/Lobby ?
Adding a random_string
does fix the issue but it works only once, as the random resource is created only once. So, I usually change the length
of the random_string
in the next iteration, so that it creates a new prefix for compute_environment_name
in every TF runs.
But when you use Terraform in CI/CD pipeline, it becomes increasingly difficult to maintain this. Hence, looking forward for name_prefix
implementation (just like in ASG).
Add keepers
to your random_string
force a change on every run.
resource "random_string" "random" {
length = 8
special = false
upper = false
keepers = {
time = "${timestamp()}"
}
}
This is not ideal since it forces a recreate when you apply
even nothing is changed on your compute environment.
Support for a new compute_environment_name_prefix
argument or completely omitting the name has been merged and will release with version 2.44.0 of the Terraform AWS Provider, Thursday next week. Thanks to @marcinwyszynski for the implementation. 👍
This has been released in version 2.44.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.
For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!
I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.
If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!
Most helpful comment
Going to try and make a pull request to add a
compute_environment_name_prefix
field. My apologies if I just missed it, but is there a slack org or some sort of forum for this plugin? Was having some trouble running a subset of the tests and figured asking for help would be faster than digging in too far.