Skip to content

Commit

Permalink
fix(sqs): imported queue ignores environment from arn (#27906)
Browse files Browse the repository at this point in the history
This PR fixes the bug that `fromQueueArn` does not extract region and account from the arn.

- Behavior until now

```ts
    const stack = new Stack(); // <- region: us-east-1, account: 123456789012
    const imported = sqs.Queue.fromQueueArn(stack, 'Imported', 'arn:aws:sns:us-west-2:999999999999:queue');

    // imported.env.region : us-east-1("Ref": "AWS::Region"). But it should be us-west-2.
    // imported.env.account : 123456789012("Ref": "AWS::AccountId"). But it should be 999999999999.
```

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
go-to-k committed Nov 10, 2023
1 parent bfbe756 commit 633dbe2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/aws-cdk-lib/aws-sqs/lib/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ export class Queue extends QueueBase {
}
}

return new Import(scope, id);
return new Import(scope, id, {
environmentFromArn: attrs.queueArn,
});
}

/**
Expand Down
32 changes: 32 additions & 0 deletions packages/aws-cdk-lib/aws-sqs/test/sqs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,38 @@ describe('export and import', () => {
});
expect(stack.resolve(imports.queueName)).toEqual('queue1');
});

test('sets account for imported queue env by fromQueueAttributes', () => {
const stack = new Stack();
const imported = sqs.Queue.fromQueueAttributes(stack, 'Imported', {
queueArn: 'arn:aws:sqs:us-west-2:999999999999:queue',
});

expect(imported.env.account).toEqual('999999999999');
});

test('sets region for imported queue env by fromQueueAttributes', () => {
const stack = new Stack();
const imported = sqs.Queue.fromQueueAttributes(stack, 'Imported', {
queueArn: 'arn:aws:sqs:us-west-2:999999999999:queue',
});

expect(imported.env.region).toEqual('us-west-2');
});

test('sets account for imported queue env by fromQueueArn', () => {
const stack = new Stack();
const imported = sqs.Queue.fromQueueArn(stack, 'Imported', 'arn:aws:sqs:us-west-2:999999999999:queue');

expect(imported.env.account).toEqual('999999999999');
});

test('sets region for imported queue env by fromQueueArn', () => {
const stack = new Stack();
const imported = sqs.Queue.fromQueueArn(stack, 'Imported', 'arn:aws:sqs:us-west-2:123456789012:queue');

expect(imported.env.region).toEqual('us-west-2');
});
});

describe('grants', () => {
Expand Down

0 comments on commit 633dbe2

Please sign in to comment.