Help us understand the problem. What is going on with this article?

terraformでaws_launch_configurationを作成する時は、name_prefixを使う

More than 1 year has passed since last update.

概要

terraformでaws_launch_configurationを更新したい時に、再作成できなくて困ったのでメモ

以下のようなコードでaws_launch_configurationを作成した。

resource "aws_launch_configuration" "config" {
  name                               = "hogehoge-config"
  image_id                    = "ami-0e95c345XXXXXXXXXXX"
  instance_type               = "t2.micro"
  key_name                    = "adminkey"
  security_groups             = ["sg-1234567890"]
  associate_public_ip_address = 0

  lifecycle {
    create_before_destroy = true
  }
}

作成したあとにAMIを入れ替えたくなったので、修正して再作成。
terraform apply したところ以下のようなエラーを受け取った。

Error: Error applying plan:

1 error(s) occurred:

* module.autoscale.aws_launch_configuration.config: 1 error(s) occurred:

* aws_launch_configuration.config: Error creating launch configuration: AlreadyExists: Launch Configuration by this name already exists - A launch configuration already exists with the name <リソース名>
        status code: 400, request id: a2a7ac89-b0ca-11e8-8187-db9a15b5b0a9

どうやら一度作ったリソース名と同じ名前のリソースを再作成できないらしい。

解決策

resource "aws_launch_configuration" "config" {
  name_prefix                        = "hogehoge-config"  // name -> name_prefixに修正
  image_id                    = "ami-0e95c345XXXXXXXXXXX"
  instance_type               = "t2.micro"
  key_name                    = "adminkey"
  security_groups             = ["sg-1234567890"]
  associate_public_ip_address = 0

  lifecycle {
    create_before_destroy = true
  }
}

として再度applyすると今度は通った。

ドキュメント

公式ドキュメントには以下のように書かれている

Launch Configurations cannot be updated after creation with the Amazon Web Service API. In order to update a Launch Configuration, Terraform will destroy the existing resource and create a replacement.
In order to effectively use a Launch Configuration resource with an AutoScaling Group resource, it's recommended to specify create_before_destroy in a lifecycle block. 
Either omit the Launch Configuration name attribute, or specify a partial name with name_prefix. 

実行後

applyを実行すると、AutoScaling Groupは新しいaws_launch_configurationを元にしたものに変更されるが、すでに起動されているインスタンスが更新されるわけではない。よって新しいaws_launch_configurationでインスタンスを更新したい場合はAutoscaleさせる必要がある。

参考

https://github.com/hashicorp/terraform/issues/3665
https://www.terraform.io/docs/providers/aws/r/launch_configuration.html

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away