Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lambda): add domainName prop to FunctionUrl #30198

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,15 @@ new CfnOutput(this, 'TheUrl', {
value: fnUrl.url,
});
```
To get the domain name from the function url, simply call `domainName` on the `FunctionUrl` construct:
```ts
...

new CfnOutput(this, 'TheDomainName', {
value: fnUrl.domainName,
});
```
The .domainName attribute will return the domain name (the function url, without the scheme and the trailing slash). For example, if fnUrl.url returns `https://*******.lambda-url.ap-southeast-2.on.aws/`, then fnUrl.domainName will return `*******.lambda-url.ap-southeast-2.on.aws`.

Calls to this URL need to be signed with SigV4.

Expand Down
16 changes: 15 additions & 1 deletion packages/aws-cdk-lib/aws-lambda/lib/function-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IFunction } from './function-base';
import { IVersion } from './lambda-version';
import { CfnUrl } from './lambda.generated';
import * as iam from '../../aws-iam';
import { Duration, IResource, Resource } from '../../core';
import { Duration, Fn, IResource, Resource } from '../../core';

/**
* The auth types for a function url
Expand Down Expand Up @@ -133,9 +133,17 @@ export interface IFunctionUrl extends IResource {
* The url of the Lambda function.
*
* @attribute FunctionUrl
* For example `https://*******.lambda-url.ap-southeast-2.on.aws/`
*/
readonly url: string;

/**
* The domain name of the Lambda Function URL
*
* For example `*******.lambda-url.ap-southeast-2.on.aws`
*/
readonly domainName: string;

/**
* The ARN of the function this URL refers to
*
Expand Down Expand Up @@ -243,6 +251,12 @@ export class FunctionUrl extends Resource implements IFunctionUrl {
}
}

get domainName(): string {
return Fn.select(
0, Fn.split('/', Fn.select(1, Fn.split('https://', this.url))),
);
}

public grantInvokeUrl(grantee: iam.IGrantable): iam.Grant {
return this.function.grantInvokeUrl(grantee);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/test/function-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,32 @@ describe('FunctionUrl', () => {
});
});

test('function url domain name', () => {
// GIVEN
const stack = new cdk.Stack();
const fn = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('hello()'),
handler: 'index.hello',
runtime: lambda.Runtime.NODEJS_14_X,
});
const aliasName = 'prod';
const alias = new lambda.Alias(stack, 'Alias', {
aliasName,
version: fn.currentVersion,
});

// WHEN
const url = new lambda.FunctionUrl(stack, 'FunctionUrl', {
function: alias,
});

// THEN
const resolved = stack.resolve(stack.toJsonString({ token: url.domainName }));
expect(resolved).toEqual({
'Fn::Join': ['', ['{"token":"', { 'Fn::Select': [0, { 'Fn::Split': ['/', { 'Fn::Select': [1, { 'Fn::Split': ['https://', { 'Fn::GetAtt': ['FunctionUrl84B92DCA', 'FunctionUrl'] }] }] }] }] }, '"}']],
});
});

test('throws when configured with Version', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Loading