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

aws_imagebuilder_image_recipe - support for user data #21763

Merged
6 changes: 6 additions & 0 deletions .changelog/21763.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
resource/aws_imagebuilder_image_recipe: Add `user_data_base64` argument
```
```release-note:enhancement
data-source/aws_imagebuilder_image_recipe: Add `user_data_base64` attribute
```
29 changes: 29 additions & 0 deletions internal/service/imagebuilder/image_recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ func ResourceImageRecipe() *schema.Resource {
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
"user_data_base64": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 21847),
func(v interface{}, name string) (warns []string, errs []error) {
s := v.(string)
if !verify.IsBase64Encoded([]byte(s)) {
errs = append(errs, fmt.Errorf(
"%s: must be base64-encoded", name,
))
}
return
},
),
},
"version": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -219,6 +237,12 @@ func resourceImageRecipeCreate(d *schema.ResourceData, meta interface{}) error {
input.Tags = Tags(tags.IgnoreAWS())
}

if v, ok := d.GetOk("user_data_base64"); ok {
input.AdditionalInstanceConfiguration = &imagebuilder.AdditionalInstanceConfiguration{
UserDataOverride: aws.String(v.(string)),
}
}

if v, ok := d.GetOk("version"); ok {
input.SemanticVersion = aws.String(v.(string))
}
Expand Down Expand Up @@ -287,6 +311,11 @@ func resourceImageRecipeRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

if imageRecipe.AdditionalInstanceConfiguration != nil {
d.Set("user_data_base64", imageRecipe.AdditionalInstanceConfiguration.UserDataOverride)
}

d.Set("version", imageRecipe.Version)
d.Set("working_directory", imageRecipe.WorkingDirectory)

Expand Down
9 changes: 9 additions & 0 deletions internal/service/imagebuilder/image_recipe_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func DataSourceImageRecipe() *schema.Resource {
Computed: true,
},
"tags": tftags.TagsSchema(),
"user_data_base64": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -159,6 +163,11 @@ func dataSourceImageRecipeRead(d *schema.ResourceData, meta interface{}) error {
d.Set("parent_image", imageRecipe.ParentImage)
d.Set("platform", imageRecipe.Platform)
d.Set("tags", KeyValueTags(imageRecipe.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map())

if imageRecipe.AdditionalInstanceConfiguration != nil {
d.Set("user_data_base64", imageRecipe.AdditionalInstanceConfiguration.UserDataOverride)
}

d.Set("version", imageRecipe.Version)
d.Set("working_directory", imageRecipe.WorkingDirectory)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAccImageBuilderImageRecipeDataSource_arn(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceName, "parent_image", resourceName, "parent_image"),
resource.TestCheckResourceAttrPair(dataSourceName, "platform", resourceName, "platform"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(dataSourceName, "user_data_base64", resourceName, "user_data_base64"),
resource.TestCheckResourceAttrPair(dataSourceName, "version", resourceName, "version"),
resource.TestCheckResourceAttrPair(dataSourceName, "working_directory", resourceName, "working_directory"),
),
Expand Down Expand Up @@ -73,9 +74,10 @@ resource "aws_imagebuilder_image_recipe" "test" {
component_arn = aws_imagebuilder_component.test.arn
}

name = %[1]q
parent_image = "arn:${data.aws_partition.current.partition}:imagebuilder:${data.aws_region.current.name}:aws:image/amazon-linux-2-x86/x.x.x"
version = "1.0.0"
name = %[1]q
parent_image = "arn:${data.aws_partition.current.partition}:imagebuilder:${data.aws_region.current.name}:aws:image/amazon-linux-2-x86/x.x.x"
version = "1.0.0"
user_data_base64 = base64encode("helloworld")
}

data "aws_imagebuilder_image_recipe" "test" {
Expand Down
44 changes: 44 additions & 0 deletions internal/service/imagebuilder/image_recipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfimagebuilder "github.com/hashicorp/terraform-provider-aws/internal/service/imagebuilder"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func TestAccImageBuilderImageRecipe_basic(t *testing.T) {
Expand Down Expand Up @@ -542,6 +543,32 @@ func TestAccImageBuilderImageRecipe_pipelineUpdateDependency(t *testing.T) {
})
}

func TestAccImageBuilderImageRecipe_userDataBase64(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_imagebuilder_image_recipe.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, imagebuilder.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckImageRecipeDestroy,
Steps: []resource.TestStep{
{
Config: testAccImageRecipeUserDataBase64Config(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckImageRecipeExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "user_data_base64", verify.Base64Encode([]byte("hello world"))),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckImageRecipeDestroy(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).ImageBuilderConn

Expand Down Expand Up @@ -1089,3 +1116,20 @@ resource "aws_imagebuilder_image_recipe" "test" {
}
`, rName))
}

func testAccImageRecipeUserDataBase64Config(rName string) string {
return acctest.ConfigCompose(
testAccImageRecipeBaseConfig(rName),
fmt.Sprintf(`
resource "aws_imagebuilder_image_recipe" "test" {
component {
component_arn = aws_imagebuilder_component.test.arn
}

name = %[1]q
parent_image = "arn:${data.aws_partition.current.partition}:imagebuilder:${data.aws_region.current.name}:aws:image/amazon-linux-2-x86/x.x.x"
version = "1.0.0"
user_data_base64 = base64encode("hello world")
}
`, rName))
}
1 change: 1 addition & 0 deletions website/docs/d/imagebuilder_image_recipe.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ In addition to all arguments above, the following attributes are exported:
* `parent_image` - Platform of the image recipe.
* `platform` - Platform of the image recipe.
* `tags` - Key-value map of resource tags for the image recipe.
* `user_data_base64` - Base64 encoded contents of user data. Commands or a command script to run when build instance is launched.
* `version` - Version of the image recipe.
* `working_directory` - The working directory used during build and test workflows.
1 change: 1 addition & 0 deletions website/docs/r/imagebuilder_image_recipe.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ The following attributes are optional:
* `block_device_mapping` - (Optional) Configuration block(s) with block device mappings for the the image recipe. Detailed below.
* `description` - (Optional) Description of the image recipe.
* `tags` - (Optional) Key-value map of resource tags for the image recipe. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
* `user_data_base64` (Optional) Base64 encoded user data. Use this to provide commands or a command script to run when you launch your build instance.
* `working_directory` - (Optional) The working directory to be used during build and test workflows.

### block_device_mapping
Expand Down