-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws-s3objectlambda): add L2 construct for S3 Object Lambda
- Loading branch information
Showing
6 changed files
with
477 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
packages/@aws-cdk/aws-s3objectlambda/lib/acess-point.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import * as lambda from '@aws-cdk/aws-lambda'; | ||
import * as s3 from '@aws-cdk/aws-s3'; | ||
import * as core from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { CfnAccessPoint } from './s3objectlambda.generated'; | ||
|
||
/** | ||
* Creates an S3 Object Lambda Access Point, which can intercept | ||
* and transform `GetObject` requests. | ||
* | ||
* @param fn The Lambda function | ||
* @param props Configuration for this Access Point | ||
*/ | ||
export interface AccessPointProps { | ||
/** | ||
* The bucket to which this access point belongs. | ||
*/ | ||
readonly bucket: s3.IBucket | ||
|
||
/** | ||
* The Lambda function used to transform objects. | ||
*/ | ||
readonly fn: lambda.IFunction | ||
|
||
/** | ||
* The name of the access point access point. | ||
*/ | ||
readonly name: string | ||
|
||
/** | ||
* Whether CloudWatch metrics are enabled for the access point. | ||
* | ||
* @default false | ||
*/ | ||
readonly cloudWatchMetricsEnabled?: boolean | ||
|
||
/** | ||
* Whether the Lambda function can process `GetObject-Range` requests. | ||
* | ||
* @default false | ||
*/ | ||
readonly supportsGetObjectRange?: boolean | ||
|
||
/** | ||
* Whether the Lambda function can process `GetObject-PartNumber` requests. | ||
* | ||
* @default false | ||
*/ | ||
readonly supportsGetObjectPartNumber?: boolean | ||
|
||
/** | ||
* Additional JSON that provides supplemental data passed to the | ||
* Lambda function on every request. | ||
* | ||
* @default - No data. | ||
*/ | ||
readonly payload?: string | ||
} | ||
|
||
/** | ||
* An S3 Object Lambda Access Point for intercepting and | ||
* transforming `GetObject` requests. | ||
*/ | ||
export class AccessPoint extends core.Resource { | ||
private readonly acessPoint: CfnAccessPoint | ||
|
||
constructor(scope: Construct, id: string, props: AccessPointProps) { | ||
super(scope, id); | ||
|
||
const supporting = new s3.CfnAccessPoint(this, 'AccessPoint', { | ||
bucket: props.bucket.bucketName, | ||
}); | ||
supporting.addPropertyOverride('Name', `${props.name}-access-point`); | ||
|
||
const allowedFeatures = []; | ||
if (props.supportsGetObjectPartNumber) { | ||
allowedFeatures.push('GetObject-PartNumber'); | ||
} | ||
if (props.supportsGetObjectRange) { | ||
allowedFeatures.push('GetObject-Range'); | ||
} | ||
|
||
this.acessPoint = new CfnAccessPoint(this, 'LambdaAccessPoint', { | ||
name: props.name.toLowerCase(), | ||
objectLambdaConfiguration: { | ||
allowedFeatures, | ||
cloudWatchMetricsEnabled: props.cloudWatchMetricsEnabled, | ||
supportingAccessPoint: supporting.getAtt('Arn').toString(), | ||
transformationConfigurations: [ | ||
{ | ||
actions: ['GetObject'], | ||
contentTransformation: { | ||
AwsLambda: { | ||
FunctionArn: props.fn.functionArn, | ||
FunctionPayload: props.payload ?? '', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
this.acessPoint.addDependsOn(supporting); | ||
|
||
props.fn.addToRolePolicy( | ||
new iam.PolicyStatement({ | ||
actions: ['s3-object-lambda:WriteGetObjectResponse'], | ||
resources: ['*'], | ||
}), | ||
); | ||
} | ||
|
||
/** | ||
* The ARN of the access point. | ||
*/ | ||
get arn(): string { | ||
return this.acessPoint.getAtt('Arn').toString(); | ||
} | ||
|
||
/** | ||
* The IPv4 DNS name of the access point. | ||
*/ | ||
get domainName(): string { | ||
const urlSuffix = this.stack.urlSuffix; | ||
return `${this.acessPoint.name}-${this.stack.account}.s3-object-lambda.${urlSuffix}`; | ||
} | ||
|
||
/** | ||
* The regional domain name of the access point. | ||
*/ | ||
get regionalDomainName(): string { | ||
const urlSuffix = this.stack.urlSuffix; | ||
const region = this.stack.region; | ||
return `${this.acessPoint.name}-${this.stack.account}.s3-object-lambda.${region}.${urlSuffix}`; | ||
} | ||
|
||
/** | ||
* The virtual hosted-style URL of an S3 object through this access point. | ||
* Specify `regional: false` at the options for non-regional URL. | ||
* @param key The S3 key of the object. If not specified, the URL of the | ||
* bucket is returned. | ||
* @param options Options for generating URL. | ||
* @returns an ObjectS3Url token | ||
*/ | ||
public virtualHostedUrlForObject(key?: string, options?: s3.VirtualHostedStyleUrlOptions): string { | ||
const domainName = options?.regional ?? true ? this.regionalDomainName : this.domainName; | ||
const prefix = `https://${domainName}`; | ||
if (typeof key !== 'string') { | ||
return prefix; | ||
} | ||
if (key.startsWith('/')) { | ||
key = key.slice(1); | ||
} | ||
if (key.endsWith('/')) { | ||
key = key.slice(0, -1); | ||
} | ||
return `${prefix}/${key}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './object-lambda'; | ||
|
||
// AWS::S3ObjectLambda CloudFormation Resources: | ||
export * from './s3objectlambda.generated'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.