Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/aws: add name_prefix option to launch config #3802

Merged
merged 1 commit into from
Nov 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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