5

I am creating a s3 bucket by pulumi typescript and want to use this bucket in EC2 User data for copying an object. Following is my Code for both.

const bucket = new aws.s3.Bucket("bucket-name", {
    acl: "private",
});

I uploaded a index.html file And here is the user data of EC2:-

const userData = 
`#!/bin/bash
sudo apt update -y
sudo apt install -y apache2 && sudo apt install -y awscli 
aws s3 cp s3://`+pulumi.interpolate`${bucket.id}`+`/index.html /var/www/html/index.html
sudo systemctl start apache2`;

I used pulumi.interpolate${bucket.id}` for calling bucket Id, but when I checked on EC2 for user data then I get following error.

aws s3 cp s3://Calling [toString] on an [Output<T>] is not supported.

To get the value of an Output<T> as an Output<string> consider either:
1: o.apply(v => `prefix${v}suffix`)
2: pulumi.interpolate `prefix${v}suffix`

can anyone help how can I call bucket name in script?

manish soni
  • 515
  • 1
  • 7
  • 19
  • I believe [bucket.s3UrlForObject()](https://docs.aws.amazon.com/cdk/api/latest/typescript/api/aws-s3/ibucket.html#aws_s3_IBucket_s3UrlForObject) is what you're looking for. – aleksxor Jun 08 '21 at 11:19

1 Answers1

3

You should put interpolate outside all the string operations

const userData = pulumi.interpolate`#!/bin/bash
sudo apt update -y
sudo apt install -y apache2 && sudo apt install -y awscli 
aws s3 cp s3://${bucket.id}/index.html /var/www/html/index.html
sudo systemctl start apache2`;

If your code, the + operation invokes toString on the output and leads to the error.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107