Skip to content

Commit

Permalink
providers/aws: add name_prefix option to launch config
Browse files Browse the repository at this point in the history
See #2911.

This adds a `name_prefix` option to `aws_launch_configuration` resources.

When specified, it is used instead of `terraform-` as the prefix for the
launch configuration.  It conflicts with `name`, so existing
functionality is unchanged.  `name` still sets the name explicitly.

Added an acceptance test, and updated the site documentation.
  • Loading branch information
Paul Forman committed Nov 7, 2015
1 parent 91aeba4 commit 4d640c6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
25 changes: 22 additions & 3 deletions builtin/providers/aws/resource_aws_launch_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,33 @@ func resourceAwsLaunchConfiguration() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// https://github.com/boto/botocore/blob/9f322b1/botocore/data/autoscaling/2011-01-01/service-2.json#L1932-L1939
value := v.(string)
if len(value) > 255 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 255 characters", k))
}
return
},
},

"name_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
// https://github.com/boto/botocore/blob/9f322b1/botocore/data/autoscaling/2011-01-01/service-2.json#L1932-L1939
// uuid is 26 characters, limit the prefix to 229.
value := v.(string)
if len(value) > 255 {
if len(value) > 229 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 255 characters", k))
"%q cannot be longer than 229 characters, name is limited to 255", k))
}
return
},
Expand Down Expand Up @@ -386,6 +403,8 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface
var lcName string
if v, ok := d.GetOk("name"); ok {
lcName = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
lcName = resource.PrefixedUniqueId(v.(string))
} else {
lcName = resource.UniqueId()
}
Expand Down
18 changes: 18 additions & 0 deletions builtin/providers/aws/resource_aws_launch_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func TestAccAWSLaunchConfiguration_basic(t *testing.T) {
"aws_launch_configuration.bar", "terraform-"),
),
},
resource.TestStep{
Config: testAccAWSLaunchConfigurationPrefixNameConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
"aws_launch_configuration.baz", "baz-"),
),
},
},
})
}
Expand Down Expand Up @@ -255,3 +263,13 @@ resource "aws_launch_configuration" "bar" {
associate_public_ip_address = false
}
`

const testAccAWSLaunchConfigurationPrefixNameConfig = `
resource "aws_launch_configuration" "baz" {
name_prefix = "baz-"
image_id = "ami-21f78e11"
instance_type = "t1.micro"
user_data = "foobar-user-data-change"
associate_public_ip_address = false
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ 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][1],
it's recommend to omit the Launch Configuration `name` attribute, and
specify `create_before_destroy` in a [lifecycle][2] block, as shown:
it's recommended to specify `create_before_destroy` in a [lifecycle][2] block.
Either omit the Launch Configuration `name` attribute, or specify a partial name
with `name_prefix`. Example:

```
resource "aws_launch_configuration" "as_conf" {
name_prefix = "terraform-lc-example-"
image_id = "ami-1234"
instance_type = "m1.small"
Expand Down Expand Up @@ -87,7 +89,9 @@ resource "aws_autoscaling_group" "bar" {
The following arguments are supported:

* `name` - (Optional) The name of the launch configuration. If you leave
this blank, Terraform will auto-generate it.
this blank, Terraform will auto-generate a unique name.
* `name_prefix` - (Optional) Creates a unique name beginning with the specified
prefix. Conflicts with `name`.
* `image_id` - (Required) The EC2 image ID to launch.
* `instance_type` - (Required) The size of instance to launch.
* `iam_instance_profile` - (Optional) The IAM instance profile to associate
Expand Down

0 comments on commit 4d640c6

Please sign in to comment.