How do we use apply properly to get strings for ou...
# typescript
f
How do we use apply properly to get strings for outputs? Struggling to work out what I'm missing here. Trying to get the connection URL from the RDS postgres output.
Copy code
export const db = new aws.rds.Instance(`${config.PROJECT_NAME}-postgres`, {
  engine: "postgres",
  instanceClass: "db.t2.small",
  allocatedStorage: 20,
  dbSubnetGroupName: dbSubnets.id,
  vpcSecurityGroupIds: [cluster.clusterSecurityGroup.id],
  name: config.POSTGRES_DB_NAME,
  username: config.POSTGRES_USERNAME,
  password: config.POSTGRES_PASSWORD,
  skipFinalSnapshot: true,
  publiclyAccessible: true
});

const username = db.username.apply(un => `${un}`);
const password = db.password.apply(pw => `${pw}`);
const address = db.address.apply(addr => `${addr}`);
const port = db.port.apply(port => `${port}`);
const name = db.name.apply(name => `${name}`);

const connectionUrl = `postgres://${username}:${password}@${address}:${port}/${name}`;

// Create a secret from the DB connection
export const dbConn = new k8s.core.v1.Secret(
  "postgres-db-conn",
  {
    data: {
      dbConnectionUrl: Buffer.from(connectionUrl).toString("base64")
    }
  },
  { provider: cluster.provider }
);
l
I think what you are after is in the docs here: https://www.pulumi.com/docs/intro/concepts/programming-model/#outputs-and-strings
Copy code
// concat takes a list of args and concatenates all of them into a single output:
const url1: Output<string> = pulumi.concat("http://", hostname, ":", port, "/");
// interpolate takes a JavaScript "template literal" and expands outputs correctly:
const url2: Output<string> = pulumi.interpolate `http://${hostname}:${port}/`;
f
Problem is that comes out as a Output<string>. Don't I need it as a string string to convert it to a base64 encoded secret.
b
can you use
yourStringOutput.apply(str => convertToBase64(str))
?
f
Brilliant. Yes that works. Thanks @limited-rainbow-51650 and @broad-helmet-79436 😄
🎉 1
b
🎉