Advanced Topics

This section includes information about AWS CDK features that most developers do not use.

Creating CloudFormation Resource Constructs

CloudFormation Resource constructs are found in the @aws-cdk/resources package. They map directly onto AWS CloudFormation resources.

Important

In general, you shouldn’t need to use this type of Constructs, unless you have special requirements or there is no Construct Library for the AWS resource you need yet. You should use other packages with higher-level constructs instead.

Construct Attributes

To reference the runtime attributes of constructs, use one of the properties available on the construct object.

The following example configures a Lambda function’s dead letter queue to use a the ARN of an Amazon SQS queue resource.

import { lambda, sqs } from '@aws-cdk/resources'

const dlq = new sqs.QueueResource(this, { name: 'DLQ' });

new lambda.FunctionResource(this, {
   deadLetterConfig: {
      targetArn: dlq.queueArn
   }
});

The aws-cdk.Resource.ref attribute represents the AWS CloudFormation resource’s intrinsic reference (or “Return Value”). For example, for queue.ref will also refer to the queue’s ARN. If possible, it is preferrable to use an explicitly named attribute instead of ref.

Resource Options

The aws-cdk.Resource.options object includes AWS CloudFormation options, such as condition, updatePolicy, createPolicy and metadata, for a resource.

Parameters

import { Parameter } from '@aws-cdk/core';

const p = new Parameter(this, 'MyParam', { type: 'String' });
new sns.TopicResource(this, 'MyTopic', { displayName: p.ref });

Outputs

import { Output } from '@aws-cdk/core';

const queue = new sqs.QueueResource(this, 'MyQueue');
const out = new Output(this, 'MyQueueArn', { value: queue.queueArn });

const import = out.makeImportValue();
assert(import === { "Fn::ImportValue": out.exportName }

Conditions

import { Condition } from '@aws-cdk/core';
const cond = new Condition(this, 'MyCondition', {
    expression: new FnIf(...)
});

const queue = new sqs.QueueResource(this, 'MyQueue');
queue.options.condition = cond;

Intrinsic Functions

import { FnJoin } from '@aws-cdk/core';
new FnJoin(",", ...)

Pseudo Parameters

import { AwsRegion } from '@aws-cdk/core';
new AwsRegion()