0
votes

I have a typescript aws cdk app having two stacks.

  1. Stack1 - deploys an api gateway and lambda proxy and connects the two. I have used CfnOutput to emit the api end point in this stack
  2. Stack2 - creates a static S3 bucket and uploads the static website files.This website has a form functionality and a 'Submit' action in script.js. The script.js, has a 'const url' parameter that is used to POST the form parameters to the api end point that I get from Stack1.

I am trying to identify an approach to take the output of Stack1 and update the api end point in script.js before upload the website as part of Stack2.

const app = new cdk.App();
const submithandlerstack = new CreateSubscriptionWebsiteSubmitHandlerStack(app, 'CreateSubscriptionWebsiteSubmitHandlerStack', 
{
  bucketname: "abcd.efgh.com"
});

console.log(Fn.importValue("SubscriptionApi"));
const fs = require('fs');

console.log("Here is the endpoint" + JSON.stringify(Fn.importValue("SubscriptionApi")));

const config1path = '../website/config1.json';
fs.writeFile(config1path, submithandlerstack.SubscriptionApi, function (err: any) 
{
  console.log("error", err);
});

const deploymenthandlerstack = new CreateSubscriptionHDeploymentHandlerStack(app, 'CreateSubscriptionHDeploymentHandlerStack', 
{
  bucketname: "abcd.efgh.com",
  apiUrl: submithandlerstack.SubscriptionApi
}).addDependency(submithandlerstack);

I am beginning to realize that this is not how 'cdk synth' works i.e. in a sequential manner. All I get is a string with placeholder values such as

//${Token[TOKEN.82]}.execute-api.${Token[AWS.Region.4]}.${Token[AWS.URLSuffix.1]}/${Token[TOKEN.100]}/

So, my question is, what is the recommended way to achieve what I described above?

If local modification of the POST url (before upload the S3 static website) is not possible, is it possible to read the post url from aws key value store, in script.js?

Thanks

1

1 Answers

0
votes

One option could be saving the file once finished every deploy

cdk deploy {stack-name} --outputs-file {output-json-file}

In that way you can extract the content from json file and adding it to your static folder before to execute the second stack