1

In the AWS CDK, it's straight forward to create a Pinpoint Service. But how do you get the Project ID (also referred to as the Pinpoint App ID or Application ID) for use in subsequent CDK code.

Create a Pinpoint project:

const pinpointProject = new pinpoint.CfnApp(this, 'PinpointNotificationProject', {
    name: 'myProject',
});

In the AWS CloudFormation docs it says: "When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the unique identifier (ApplicationId) for the Amazon Pinpoint application."

However, the following CDK code returns the project name not the id. The value of logicalId = myProject.

cdk.Fn.ref(pinpointProject.logicalId); // This returns 'myProject'
pinpointProject.ref; // This also returns 'myProject'
Matthew
  • 2,871
  • 5
  • 34
  • 59

2 Answers2

3

This is confirmed fixed in the latest CDK version 1.130.0. The ref property now returns the Pinpoint ProjectId.

Matthew
  • 2,871
  • 5
  • 34
  • 59
  • 1
    This works but I would never have guessed this holds that app/project id based on any official documentation I've found. – dreamwagon Aug 03 '22 at 20:19
1

The problem you are running into is that pinpoint is not a finished module. You can see this that all the functions within are prefixed with Cfn - cloudformation. This means that they are barebones and not tied into all the interface hooks that the rest of CDK is making use of to toss things around.

First, the logical ID is NOT the project name. the Logical Id is part of the Cloudformation Template that is generated for any resource Cloudformation is going to stand up. It links the given resource to the stack, so that any changes under the same logical id will be applied to the same stood up resource. It is only referenced internally to the cloudformation stack and never known outside. CDK uses the LogicalID to generate the name of the resource if you do not specify one.

Second, Taking a look at the documentation shows that CfnApp has the following property: attrArn. Meaning in your code, you would reference this by pinpointProject.attrArn - the arn of a pinpoint resource is something like: arn:aws:mobiletargeting:region:accountId:apps/projectId. with, as you guessed it, the projectId as the last value. you can split the string and get that value out, or use the arn manipulation methods provided as part of the core module to extract what you need.

Finally, even though the Pinpoint module is pretty much just barebones, it may still be possible to pass the variable storing your Pinpoint Construct Object to whatever other resource requires it. I say may because, as mentioned, most of the Cfn prefixed functions do not have the proper hooks to do this well - but some do, and Ive never worked with Pinpoint directly.

I recommend spending some time to understand how the CDK Documentation is laid out. Its bare bones in places, but once you understand how they structured it, these kinds of questions are readily answered within.

lynkfox
  • 2,003
  • 1
  • 8
  • 16
  • 1
    Based on the CDK documentation L1 / CFN-RESOURCES is stable. There's nothing indicating it's not finished and I would expected the ClourFormation docs to hold true. Yes - there are no L2 / CDK-CONSTRUCTS. But it seems like an AWS oversight not to have easy access to one of the most important variables. I've written a bunch of CDK stacks which required all sorts of interesting workarounds but haven't hit this specific issue before -- so this workaround is very helpful and the arn is available as you've documented. – Matthew Nov 05 '21 at 14:03
  • I did not say it was experimental. But, as you say, there are no L2 Constructs yet - and those are the end goal of all of CDK, to tie them all together with those beautiful hooks. So, its stable - but very much not finished I dont know for certain, but I think the Cfn functions are automatically generated based on the CloudFormation properties - and then cleaned up of course, but they all have the same basic structure of attributes/methods and they are experimenting with a lot of automatic reflection creation in the CDK (see all non Typescript CDK libraries :grin:) – lynkfox Nov 05 '21 at 14:10
  • 1
    Like you say - there are some *very* interesting workarounds you have to manage with CDK right now, and will probably be that way for a long while. But hey - at least we can write them! Glad it helped, apologies if i came off as condescending – lynkfox Nov 05 '21 at 14:10