Skip to content

Commit 6dd30ea

Browse files
authored
Added user_data_base64 (fixed #117) (#137)
1 parent 1a1bdb8 commit 6dd30ea

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ data "aws_ami" "ubuntu-xenial" {
124124
| tags | A mapping of tags to assign to the resource | map(string) | `{}` | no |
125125
| tenancy | The tenancy of the instance (if the instance is running in a VPC). Available values: default, dedicated, host. | string | `"default"` | no |
126126
| use\_num\_suffix | Always append numerical suffix to instance name, even if instance_count is 1 | bool | `"false"` | no |
127-
| user\_data | The user data to provide when launching the instance | string | `""` | no |
127+
| user\_data | The user data to provide when launching the instance. Do not pass gzip-compressed data via this argument; see user_data_base64 instead. | string | `"null"` | no |
128+
| user\_data\_base64 | Can be used instead of user_data to pass base64-encoded binary data directly. Use this instead of user_data whenever the value is not a valid UTF-8 string. For example, gzip-encoded user data must be base64-encoded and passed via this argument to avoid corruption. | string | `"null"` | no |
128129
| volume\_tags | A mapping of tags to assign to the devices created by the instance at launch time | map(string) | `{}` | no |
129130
| vpc\_security\_group\_ids | A list of security group IDs to associate with | list(string) | `"null"` | no |
130131

examples/basic/main.tf

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ provider "aws" {
22
region = "eu-west-1"
33
}
44

5+
locals {
6+
user_data = <<EOF
7+
#!/bin/bash
8+
echo "Hello Terraform!"
9+
EOF
10+
}
11+
512
##################################################################
613
# Data sources to get VPC, subnet, security group and AMI details
714
##################################################################
@@ -70,7 +77,7 @@ resource "aws_network_interface" "this" {
7077
module "ec2" {
7178
source = "../../"
7279

73-
instance_count = 2
80+
instance_count = 1
7481

7582
name = "example-normal"
7683
ami = data.aws_ami.amazon_linux.id
@@ -81,6 +88,8 @@ module "ec2" {
8188
associate_public_ip_address = true
8289
placement_group = aws_placement_group.web.id
8390

91+
user_data_base64 = base64encode(local.user_data)
92+
8493
root_block_device = [
8594
{
8695
volume_type = "gp2"

main.tf

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ locals {
55
resource "aws_instance" "this" {
66
count = var.instance_count
77

8-
ami = var.ami
9-
instance_type = var.instance_type
10-
user_data = var.user_data
8+
ami = var.ami
9+
instance_type = var.instance_type
10+
user_data = var.user_data
11+
user_data_base64 = var.user_data_base64
1112
subnet_id = length(var.network_interface) > 0 ? null : element(
1213
distinct(compact(concat([var.subnet_id], var.subnet_ids))),
1314
count.index,

variables.tf

+8-2
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,15 @@ variable "source_dest_check" {
110110
}
111111

112112
variable "user_data" {
113-
description = "The user data to provide when launching the instance"
113+
description = "The user data to provide when launching the instance. Do not pass gzip-compressed data via this argument; see user_data_base64 instead."
114114
type = string
115-
default = ""
115+
default = null
116+
}
117+
118+
variable "user_data_base64" {
119+
description = "Can be used instead of user_data to pass base64-encoded binary data directly. Use this instead of user_data whenever the value is not a valid UTF-8 string. For example, gzip-encoded user data must be base64-encoded and passed via this argument to avoid corruption."
120+
type = string
121+
default = null
116122
}
117123

118124
variable "iam_instance_profile" {

0 commit comments

Comments
 (0)