Skip to content

Commit

Permalink
add utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
duarten committed Jul 30, 2021
1 parent abdaee0 commit b8b70b1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 35 deletions.
61 changes: 57 additions & 4 deletions packages/@aws-cdk/aws-s3objectlambda/lib/object-lambda.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 { Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnAccessPoint } from './s3objectlambda.generated';

Expand Down Expand Up @@ -60,10 +61,15 @@ export interface ObjectLambdaProps {
* An S3 Object Lambda for intercepting and transforming `GetObject` requests.
*/
export class ObjectLambda extends CoreConstruct {
private readonly objectLambda: CfnAccessPoint
private readonly stack: Stack

constructor(scope: Construct, id: string, props: ObjectLambdaProps) {
super(scope, id);

const supporting = new s3.CfnAccessPoint(this, 'access-point', {
this.stack = props.bucket.stack;

const supporting = new s3.CfnAccessPoint(this, 'AccessPoint', {
bucket: props.bucket.bucketName,
// TODO: configure publicAccessBlockConfiguration?
});
Expand All @@ -76,8 +82,8 @@ export class ObjectLambda extends CoreConstruct {
allowedFeatures.push('GetObject-Range');
}

const objectLambda = new CfnAccessPoint(this, 'lambda-access-point', {
name: `${props.fn.functionName}-access-point`,
this.objectLambda = new CfnAccessPoint(this, 'LambdaAccessPoint', {
name: `${props.fn.functionName.toLowerCase()}-access-point`,
objectLambdaConfiguration: {
allowedFeatures,
cloudWatchMetricsEnabled: props.cloudWatchMetricsEnabled,
Expand All @@ -95,7 +101,7 @@ export class ObjectLambda extends CoreConstruct {
],
},
});
objectLambda.addDependsOn(supporting);
this.objectLambda.addDependsOn(supporting);

props.fn.addToRolePolicy(
new iam.PolicyStatement({
Expand All @@ -104,4 +110,51 @@ export class ObjectLambda extends CoreConstruct {
}),
);
}

/**
* The ARN of the Object Lambda access point.
*/
get arn(): string {
return this.objectLambda.getAtt('Arn').toString();
}

/**
* The IPv4 DNS name of the Object Lambda access point.
*/
get domainName(): string {
const urlSuffix = this.stack.urlSuffix;
return `${this.objectLambda.name}-${this.stack.account}.s3.${urlSuffix}`;
}

/**
* The regional domain name of the Object Lambda access point.
*/
get regionalDomainName(): string {
const urlSuffix = this.stack.urlSuffix;
const region = this.stack.region;
return `${this.objectLambda.name}-${this.stack.account}.s3.${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}`;
}
}
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-s3objectlambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@
"publishConfig": {
"tag": "latest"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,36 +149,26 @@
"MyFunction2ServiceRole07E5BE0E"
]
},
"MyObjectLambda1accesspoint2C407F9B": {
"MyObjectLambda1AccessPointD5812646": {
"Type": "AWS::S3::AccessPoint",
"Properties": {
"Bucket": {
"Ref": "MyBucketF68F3FF0"
}
}
},
"MyObjectLambda1lambdaaccesspoint742618A6": {
"MyObjectLambda1LambdaAccessPoint73C4BD68": {
"Type": "AWS::S3ObjectLambda::AccessPoint",
"Properties": {
"Name": {
"Fn::Join": [
"",
[
{
"Ref": "MyFunction12A744C2E"
},
"-access-point"
]
]
},
"Name": "${token[token.87]}-access-point",
"ObjectLambdaConfiguration": {
"AllowedFeatures": [
"GetObject-PartNumber"
],
"CloudWatchMetricsEnabled": true,
"SupportingAccessPoint": {
"Fn::GetAtt": [
"MyObjectLambda1accesspoint2C407F9B",
"MyObjectLambda1AccessPointD5812646",
"Arn"
]
},
Expand All @@ -203,38 +193,28 @@
}
},
"DependsOn": [
"MyObjectLambda1accesspoint2C407F9B"
"MyObjectLambda1AccessPointD5812646"
]
},
"MyObjectLambda2accesspoint757C55B0": {
"MyObjectLambda2AccessPoint76FB5ACF": {
"Type": "AWS::S3::AccessPoint",
"Properties": {
"Bucket": {
"Ref": "MyBucketF68F3FF0"
}
}
},
"MyObjectLambda2lambdaaccesspoint16890151": {
"MyObjectLambda2LambdaAccessPoint1043EB83": {
"Type": "AWS::S3ObjectLambda::AccessPoint",
"Properties": {
"Name": {
"Fn::Join": [
"",
[
{
"Ref": "MyFunction2F2A964CA"
},
"-access-point"
]
]
},
"Name": "${token[token.104]}-access-point",
"ObjectLambdaConfiguration": {
"AllowedFeatures": [
"GetObject-Range"
],
"SupportingAccessPoint": {
"Fn::GetAtt": [
"MyObjectLambda2accesspoint757C55B0",
"MyObjectLambda2AccessPoint76FB5ACF",
"Arn"
]
},
Expand All @@ -259,7 +239,7 @@
}
},
"DependsOn": [
"MyObjectLambda2accesspoint757C55B0"
"MyObjectLambda2AccessPoint76FB5ACF"
]
}
}
Expand Down

0 comments on commit b8b70b1

Please sign in to comment.