providers/aws: add name_prefix option to launch config
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.
This commit is contained in:
parent
91aeba48ff
commit
4d640c6528
|
@ -26,10 +26,11 @@ func resourceAwsLaunchConfiguration() *schema.Resource {
|
||||||
|
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
"name": &schema.Schema{
|
"name": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
|
ConflictsWith: []string{"name_prefix"},
|
||||||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
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
|
// https://github.com/boto/botocore/blob/9f322b1/botocore/data/autoscaling/2011-01-01/service-2.json#L1932-L1939
|
||||||
value := v.(string)
|
value := v.(string)
|
||||||
|
@ -41,6 +42,22 @@ func resourceAwsLaunchConfiguration() *schema.Resource {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"name_prefix": &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: 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) > 229 {
|
||||||
|
errors = append(errors, fmt.Errorf(
|
||||||
|
"%q cannot be longer than 229 characters, name is limited to 255", k))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
"image_id": &schema.Schema{
|
"image_id": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
|
@ -386,6 +403,8 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface
|
||||||
var lcName string
|
var lcName string
|
||||||
if v, ok := d.GetOk("name"); ok {
|
if v, ok := d.GetOk("name"); ok {
|
||||||
lcName = v.(string)
|
lcName = v.(string)
|
||||||
|
} else if v, ok := d.GetOk("name_prefix"); ok {
|
||||||
|
lcName = resource.PrefixedUniqueId(v.(string))
|
||||||
} else {
|
} else {
|
||||||
lcName = resource.UniqueId()
|
lcName = resource.UniqueId()
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,14 @@ func TestAccAWSLaunchConfiguration_basic(t *testing.T) {
|
||||||
"aws_launch_configuration.bar", "terraform-"),
|
"aws_launch_configuration.bar", "terraform-"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
resource.TestStep{
|
||||||
|
Config: testAccAWSLaunchConfigurationPrefixNameConfig,
|
||||||
|
Check: resource.ComposeTestCheckFunc(
|
||||||
|
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
|
||||||
|
testAccCheckAWSLaunchConfigurationGeneratedNamePrefix(
|
||||||
|
"aws_launch_configuration.baz", "baz-"),
|
||||||
|
),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -255,3 +263,13 @@ resource "aws_launch_configuration" "bar" {
|
||||||
associate_public_ip_address = false
|
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
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
|
@ -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
|
Web Service API. In order to update a Launch Configuration, Terraform will
|
||||||
destroy the existing resource and create a replacement. In order to effectively
|
destroy the existing resource and create a replacement. In order to effectively
|
||||||
use a Launch Configuration resource with an [AutoScaling Group resource][1],
|
use a Launch Configuration resource with an [AutoScaling Group resource][1],
|
||||||
it's recommend to omit the Launch Configuration `name` attribute, and
|
it's recommended to specify `create_before_destroy` in a [lifecycle][2] block.
|
||||||
specify `create_before_destroy` in a [lifecycle][2] block, as shown:
|
Either omit the Launch Configuration `name` attribute, or specify a partial name
|
||||||
|
with `name_prefix`. Example:
|
||||||
|
|
||||||
```
|
```
|
||||||
resource "aws_launch_configuration" "as_conf" {
|
resource "aws_launch_configuration" "as_conf" {
|
||||||
|
name_prefix = "terraform-lc-example-"
|
||||||
image_id = "ami-1234"
|
image_id = "ami-1234"
|
||||||
instance_type = "m1.small"
|
instance_type = "m1.small"
|
||||||
|
|
||||||
|
@ -87,7 +89,9 @@ resource "aws_autoscaling_group" "bar" {
|
||||||
The following arguments are supported:
|
The following arguments are supported:
|
||||||
|
|
||||||
* `name` - (Optional) The name of the launch configuration. If you leave
|
* `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.
|
* `image_id` - (Required) The EC2 image ID to launch.
|
||||||
* `instance_type` - (Required) The size of instance to launch.
|
* `instance_type` - (Required) The size of instance to launch.
|
||||||
* `iam_instance_profile` - (Optional) The IAM instance profile to associate
|
* `iam_instance_profile` - (Optional) The IAM instance profile to associate
|
||||||
|
|
Loading…
Reference in New Issue