Skip to content
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
9 changes: 9 additions & 0 deletions .web-docs/components/builder/instance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ The configuration arguments for the builder. Arguments can either be required or
for testing provisioner logic without incurring the cost of image creation.
Defaults to `false`.

- `user_data` (string) - User data for instance initialization systems such as cloud-init. The
value is a UTF-8 string and will be Base64-encoded by the plugin before
transmission. The maximum size is 32 KiB, measured before encoding. Use
Packer's built-in functions such as `file` to read content from disk.
Packer does not wait for user data to finish executing before shutting
down the instance. If your user data must complete before the image is
created, run `cloud-init status --wait` or an equivalent in a
provisioner.

<!-- End of code generated from the comments of the Config struct in component/builder/instance/config.go; -->


Expand Down
17 changes: 17 additions & 0 deletions component/builder/instance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ type Config struct {
// Defaults to `false`.
SkipCreateImage bool `mapstructure:"skip_create_image" required:"false"`

// User data for instance initialization systems such as cloud-init. The
// value is a UTF-8 string and will be Base64-encoded by the plugin before
// transmission. The maximum size is 32 KiB, measured before encoding. Use
// Packer's built-in functions such as `file` to read content from disk.
// Packer does not wait for user data to finish executing before shutting
// down the instance. If your user data must complete before the image is
// created, run `cloud-init status --wait` or an equivalent in a
// provisioner.
UserData string `mapstructure:"user_data" required:"false"`

ctx interpolate.Context
}

Expand Down Expand Up @@ -199,6 +209,13 @@ func (c *Config) Prepare(args ...any) ([]string, error) {
)
}

if len(c.UserData) > 32*1024 {
multiErr = packer.MultiErrorAppend(
multiErr,
errors.New("user_data must be 32 KiB or less of unencoded data"),
)
}

if multiErr != nil && len(multiErr.Errors) > 0 {
return nil, multiErr
}
Expand Down
2 changes: 2 additions & 0 deletions component/builder/instance/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions component/builder/instance/step_instance_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package instance

import (
"context"
"encoding/base64"
"fmt"
"time"

Expand Down Expand Up @@ -111,6 +112,12 @@ func (o *stepInstanceCreate) Run(

return res
}(config.SSHPublicKeys),
UserData: func(userData string) string {
if userData == "" {
return ""
}
return base64.StdEncoding.EncodeToString([]byte(userData))
}(config.UserData),
},
})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@
for testing provisioner logic without incurring the cost of image creation.
Defaults to `false`.

- `user_data` (string) - User data for instance initialization systems such as cloud-init. The
value is a UTF-8 string and will be Base64-encoded by the plugin before
transmission. The maximum size is 32 KiB, measured before encoding. Use
Packer's built-in functions such as `file` to read content from disk.
Packer does not wait for user data to finish executing before shutting
down the instance. If your user data must complete before the image is
created, run `cloud-init status --wait` or an equivalent in a
provisioner.

<!-- End of code generated from the comments of the Config struct in component/builder/instance/config.go; -->
Loading