aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.68k stars 3.93k forks source link

Resolve parameters using the AWS API at deploy time? #3941

Closed pierreis closed 5 years ago

pierreis commented 5 years ago

:question: General Issue

The Question

The CDK currently implements deploy-time resolution for certain pieces of data, such as SSM parameters. Is there a specific API we can use to call the AWS API programmatically at deploy time to get some custom information?

My use-case is obtaining the Elemental MediaConvert endpoint at deploy time, in order to set it as a lambda environment variable.

Environment

rhboyd commented 5 years ago

My understanding is that the idiomatic CDK way of doing this would be to add a AwsCustomResource to perform SDK calls at Deploy Time.

https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_custom-resources.AwsCustomResource.html

pierreis commented 5 years ago

Okay, thanks. The doc is not very clear IMHO. Here is what I tried:

// Get MediaConvert endpoint
const getParameter = new AwsCustomResource(this, 'GetMediaConvertEndpoint', {
  onUpdate: {
    service: 'MediaConvert',
    action: 'describeEndpoints',
    parameters: {
      MaxResults: 0,
    },
    physicalResourceIdPath: 'Endpoints.0.Url',
  }
});

// Handler
const handler = new Function(this, "MediaHandler", {
  runtime: Runtime.NODEJS_8_10,
  code: Code.asset('../resources'),
  handler: 'process.main',
  environment: {
    MEDIA_ENDPOINT: getParameter.getData('Endpoints.0.Url')  // Error here: IResolvable is not assignable to string
  }
});

I am getting a TS compilation error:

Type '{ MEDIA_ENDPOINT: IResolvable; }' is not assignable to type '{ [key: string]: string; }'.
  Property 'MEDIA_ENDPOINT' is incompatible with index signature.
    Type 'IResolvable' is not assignable to type 'string'.ts(2322)

I cannot really find any indication on how to resolve Resolvable values at deploy time.

pierreis commented 5 years ago

Ok, it was just a missing toString() call on the resolvable. I would suggest the docs to make this clear with some example. The ones for custom resources seem to be pretty outdated.

SomayaB commented 5 years ago

Hi @pierreis, I'm glad your issue got resolved! 👍 I will close this issue. If you want to, feel free to open a PR to update the docs. If not, you can simply open a new issue to specifically address the docs being unclear.