https://pulumi.com logo
#golang
Title
# golang
q

quiet-lock-69596

03/28/2021, 2:56 PM
Hey, I’m quite new to Go and Pulumi. So this is prolly a quite easy question. I’ve been looking into both https://github.com/pulumi/automation-api-examples/blob/main/go/local_program/fargate/main.go and https://www.pulumi.com/docs/reference/pkg/aws/ecs/taskdefinition/#inputs and https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html but I seem to fail miserable anyhow. I’m trying to apply some environment variables and secrets to my container. But I end up getting the following error:
Copy code
error: aws:ecs/taskDefinition:TaskDefinition resource 'api-task' has a problem: ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal number into Go struct field KeyValuePair.Environment.Value of type string. Examine values at 'TaskDefinition.ContainerDefinitions'.
So to my understanding is that my type conversion is wrong in some way. Can anybody maybe point me in the right direction or to some example covering how to add the variables. Snippet of my code:
Copy code
tmpContainerDef, err := json.Marshal([]map[string]interface{}{
			{
				"name":      pulumi.Sprintf("%q", image.ImageName),
				"image":     pulumi.Sprintf("%q", image),
				"cpu":       256,
				"memory":    512,
				"portMappings": []map[string]interface{}{
					{
						"containerPort": 8888,
					},
				},
				"environment": []map[string]interface{}{
					{
						"name": "APP_ENV",
						"value": "prod",
					},
					{
						"name": "API_PORT",
						"value": 8888,
					},
					{
						"name": "DB_DRIVER",
						"value": "postgres",
					},
					{
						"name": "DB_HOST",
						"value": pulumi.Sprintf("%q", dbCluster.Endpoint),
					},
					{
						"name": "DB_NAME",
						"value": pulumi.Sprintf("%q", dbCluster.DatabaseName),
					},
					{
						"name": "DB_USER",
						"value": pulumi.Sprintf("%q", dbCluster.MasterUsername),
					},
					{
						"name": "DB_PORT",
						"value": pulumi.Sprintf("%d", dbCluster.Port),
					},
				},
				"secret": []map[string]interface{}{
					{
						"name": "API_SECRET",
						"valueFrom": pulumi.Sprintf("%q", apiSecret.Arn),
					},
					{
						"name": "DB_PASSWORD",
						"valueFrom": pulumi.Sprintf("%q", dbSecret.Arn),
					},
				},
			},
		})
		if err != nil {
			return err
		}
b

billowy-army-68599

03/28/2021, 6:55 PM
The ECS JSON takes a string, and the
dbSecret.Arn
is an eventual value, so you'll need to access it via an
ApplyT
Here's an example: https://github.com/jaxxstorm/iac-in-go/blob/fb5eb2c35ed9b4498b35701f3a3e4a43d6c896f7/bastion/main.go#L67-L93
q

quiet-lock-69596

03/28/2021, 7:44 PM
Gotcha. So pretty much like https://github.com/pulumi/automation-api-examples/blob/main/go/local_program/fargate/main.go#L152-L163 Is there a way to run
ApplyT
with several values? Since I need more than just
dbSecret.Arn
as eventual values.
b

billowy-army-68599

03/28/2021, 8:36 PM
q

quiet-lock-69596

03/29/2021, 5:50 AM
Great! Thx for helping me out @billowy-army-68599! Really appreciate the support 🚀
3 Views