Skip to content

Commit

Permalink
add annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
pahud committed Aug 17, 2024
1 parent 9558a4d commit 1548fa3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions packages/aws-cdk-lib/aws-ssm/lib/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as cxschema from '../../cloud-assembly-schema';
import {
Annotations,
CfnDynamicReference, CfnDynamicReferenceService, CfnParameter,
ContextProvider, Fn, IResource, Resource, Stack, Token,
Tokenization,
Expand Down Expand Up @@ -488,7 +489,11 @@ export class StringParameter extends ParameterBase implements IStringParameter {
const stackRegion = Stack.of(scope).region;
if (arnParts.length !== 6) {
throw new Error('unexpected StringParameterArn format');
} else if (Token.isUnresolved(stackRegion)) {
// Region is unknown during synthesis, emit a warning for visibility
Annotations.of(scope).addWarningV2('aws-cdk-lib/aws-ssm:crossAccountReferenceSameRegion', 'Cross-account references will only work within the same region');
} else if (!Token.isUnresolved(stackRegion) && arnParts[3] !== stackRegion) {
// If the region is known, it must match the region specified in the ARN string
throw new Error('stringParameterArn must be in the same region as the stack');
}

Expand Down
13 changes: 7 additions & 6 deletions packages/aws-cdk-lib/aws-ssm/test/parameter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-len */

import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import { Template } from '../../assertions';
import { Template, Annotations } from '../../assertions';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as cdk from '../../core';
Expand Down Expand Up @@ -422,15 +422,16 @@ test('fromStringParameterArn does not throw error when StringParameterArn is in
}).not.toThrow();
});

test('fromStringParameterArn does not throw error when stack region is unresolved', () => {
test('fromStringParameterArn emits an annotation when stack region is unresolved', () => {
// GIVEN
const stack = new cdk.Stack();
const stack = new cdk.Stack(undefined, 'Stack');
const sameRegionArn = 'arn:aws:ssm:us-east-1:123456789012:parameter/dummyName';

// WHEN
ssm.StringParameter.fromStringParameterArn(stack, 'MyParamName', sameRegionArn);

// THEN
expect(() => {
ssm.StringParameter.fromStringParameterArn(stack, 'MyParamName', sameRegionArn);
}).not.toThrow();
Annotations.fromStack(stack).hasWarning('/Stack', 'Cross-account references will only work within the same region [ack: aws-cdk-lib/aws-ssm:crossAccountReferenceSameRegion]');
});

test('StringParameter.fromStringParameterAttributes', () => {
Expand Down

0 comments on commit 1548fa3

Please sign in to comment.