Skip to content

Commit

Permalink
Merge branch 'main' into dual-stack-ipv6-feature-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jan 7, 2025
2 parents ea5586a + 946b748 commit 9e01387
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 183 deletions.
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-s3objectlambda-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,16 @@ new s3objectlambda.AccessPoint(stack, 'MyObjectLambda', {
},
});
```

## Accessing the S3 AccessPoint ARN

If you need access to the s3 accesspoint, you can get its ARN like so:

```ts
import * as s3objectlambda from '@aws-cdk/aws-s3objectlambda-alpha';

declare const accessPoint: s3objectlambda.AccessPoint;
const s3AccessPointArn = accessPoint.s3AccessPointArn;
```

This is only supported for AccessPoints created in the stack - currently you're unable to get the S3 AccessPoint ARN for imported AccessPoints. To do that you'd have to know the S3 bucket name beforehand.
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ export class AccessPoint extends AccessPointBase {
*/
public readonly accessPointCreationDate: string;

/**
* The ARN of the S3 access point.
*/
public readonly s3AccessPointArn: string;

constructor(scope: Construct, id: string, props: AccessPointProps) {
super(scope, id, {
physicalName: props.accessPointName,
Expand Down Expand Up @@ -241,6 +246,7 @@ export class AccessPoint extends AccessPointBase {
],
},
});
this.s3AccessPointArn = supporting.attrArn;
this.accessPointName = accessPoint.ref;
this.accessPointArn = accessPoint.attrArn;
this.accessPointCreationDate = accessPoint.attrCreationDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ test('Can create a valid access point', () => {
regional: false,
}),
});
new cdk.CfnOutput(stack, 'S3AccessPointArn', {
value: accessPoint.s3AccessPointArn,
});

expect(Template.fromStack(stack).findOutputs('*')).toEqual(
{
Expand Down Expand Up @@ -98,6 +101,14 @@ test('Can create a valid access point', () => {
],
},
},
S3AccessPointArn: {
Value: {
'Fn::GetAtt': [
'MyObjectLambdaSupportingAccessPointA2D2026E',
'Arn',
],
},
},
VirtualHostedRegionalUrl: {
Value: {
'Fn::Join': [
Expand Down
125 changes: 1 addition & 124 deletions packages/aws-cdk/lib/api/cxapp/cloud-executable.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { promises as fs } from 'fs';
import * as cxapi from '@aws-cdk/cx-api';
import { RegionInfo } from '@aws-cdk/region-info';
import * as semver from 'semver';
import { CloudAssembly } from './cloud-assembly';
import * as contextproviders from '../../context-providers';
import { debug, warning } from '../../logging';
import { debug } from '../../logging';
import { Configuration } from '../../settings';
import { ToolkitError } from '../../toolkit/error';
import { SdkProvider } from '../aws-auth';
Expand All @@ -14,13 +11,6 @@ import { SdkProvider } from '../aws-auth';
*/
export type Synthesizer = (aws: SdkProvider, config: Configuration) => Promise<cxapi.CloudAssembly>;

/**
* The Cloud Assembly schema version where the framework started to generate analytics itself
*
* See https://github.com/aws/aws-cdk/pull/10306
*/
const TEMPLATE_INCLUDES_ANALYTICS_SCHEMA_VERSION = '6.0.0';

export interface CloudExecutableProps {
/**
* Application configuration (settings and context)
Expand Down Expand Up @@ -69,8 +59,6 @@ export class CloudExecutable {
}

private async doSynthesize(): Promise<CloudAssembly> {
const trackVersions: boolean = this.props.configuration.settings.get(['versionReporting']);

// We may need to run the cloud executable multiple times in order to satisfy all missing context
// (When the executable runs, it will tell us about context it wants to use
// but it missing. We'll then look up the context and run the executable again, and
Expand Down Expand Up @@ -113,76 +101,10 @@ export class CloudExecutable {
}
}

if (trackVersions && !semver.gte(assembly.version, TEMPLATE_INCLUDES_ANALYTICS_SCHEMA_VERSION)) {
// @deprecate(v2): the framework now manages its own analytics. For
// Cloud Assemblies *older* than when we introduced this feature, have
// the CLI add it. Otherwise, do nothing.
await this.addMetadataResource(assembly);
}

return new CloudAssembly(assembly);
}
}

/**
* Modify the templates in the assembly in-place to add metadata resource declarations
*/
private async addMetadataResource(rootAssembly: cxapi.CloudAssembly) {
if (!rootAssembly.runtime) { return; }

const modules = formatModules(rootAssembly.runtime);
await processAssembly(rootAssembly);

async function processAssembly(assembly: cxapi.CloudAssembly) {
for (const stack of assembly.stacks) {
await processStack(stack);
}
for (const nested of assembly.nestedAssemblies) {
await processAssembly(nested.nestedAssembly);
}
}

async function processStack(stack: cxapi.CloudFormationStackArtifact) {
const resourcePresent = stack.environment.region === cxapi.UNKNOWN_REGION
|| RegionInfo.get(stack.environment.region).cdkMetadataResourceAvailable;
if (!resourcePresent) { return; }

if (!stack.template.Resources) {
stack.template.Resources = {};
}
if (stack.template.Resources.CDKMetadata) {
// Already added by framework, this is expected.
return;
}

stack.template.Resources.CDKMetadata = {
Type: 'AWS::CDK::Metadata',
Properties: {
Modules: modules,
},
};

if (stack.environment.region === cxapi.UNKNOWN_REGION) {
stack.template.Conditions = stack.template.Conditions || {};
const condName = 'CDKMetadataAvailable';
if (!stack.template.Conditions[condName]) {
stack.template.Conditions[condName] = _makeCdkMetadataAvailableCondition();
stack.template.Resources.CDKMetadata.Condition = condName;
} else {
warning(`The stack ${stack.id} already includes a ${condName} condition`);
}
}

// The template has changed in-memory, but the file on disk remains unchanged so far.
// The CLI *might* later on deploy the in-memory version (if it's <50kB) or use the
// on-disk version (if it's >50kB).
//
// Be sure to flush the changes we just made back to disk. The on-disk format is always
// JSON.
await fs.writeFile(stack.templateFullPath, JSON.stringify(stack.template, undefined, 2), { encoding: 'utf-8' });
}
}

private get canLookup() {
return !!(this.props.configuration.settings.get(['lookups']) ?? true);
}
Expand All @@ -202,48 +124,3 @@ function setsEqual<A>(a: Set<A>, b: Set<A>) {
}
return true;
}

function _makeCdkMetadataAvailableCondition() {
return _fnOr(RegionInfo.regions
.filter(ri => ri.cdkMetadataResourceAvailable)
.map(ri => ({ 'Fn::Equals': [{ Ref: 'AWS::Region' }, ri.name] })));
}

/**
* This takes a bunch of operands and crafts an `Fn::Or` for those. Funny thing is `Fn::Or` requires
* at least 2 operands and at most 10 operands, so we have to... do this.
*/
function _fnOr(operands: any[]): any {
if (operands.length === 0) {
throw new ToolkitError('Cannot build `Fn::Or` with zero operands!');
}
if (operands.length === 1) {
return operands[0];
}
if (operands.length <= 10) {
return { 'Fn::Or': operands };
}
return _fnOr(_inGroupsOf(operands, 10).map(group => _fnOr(group)));
}

function _inGroupsOf<T>(array: T[], maxGroup: number): T[][] {
const result = new Array<T[]>();
for (let i = 0; i < array.length; i += maxGroup) {
result.push(array.slice(i, i + maxGroup));
}
return result;
}

function formatModules(runtime: cxapi.RuntimeInfo): string {
const modules = new Array<string>();

// inject toolkit version to list of modules
// eslint-disable-next-line @typescript-eslint/no-require-imports
const toolkitVersion = require('../../../package.json').version;
modules.push(`aws-cdk=${toolkitVersion}`);

for (const key of Object.keys(runtime.libraries).sort()) {
modules.push(`${key}=${runtime.libraries[key]}`);
}
return modules.join(',');
}
59 changes: 0 additions & 59 deletions packages/aws-cdk/test/api/cloud-executable.test.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,10 @@
/* eslint-disable import/order */
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import { DefaultSelection } from '../../lib/api/cxapp/cloud-assembly';
import { registerContextProvider } from '../../lib/context-providers';
import { MockCloudExecutable } from '../util';

// Apps on this version of the cxschema don't emit their own metadata resources
// yet, so rely on the CLI to add the Metadata resource in.
const SCHEMA_VERSION_THAT_DOESNT_INCLUDE_METADATA_ITSELF = '2.0.0';

describe('AWS::CDK::Metadata', () => {
test('is generated for relocatable stacks from old frameworks', async () => {
const cx = await testCloudExecutable({
env: `aws://${cxapi.UNKNOWN_ACCOUNT}/${cxapi.UNKNOWN_REGION}`,
versionReporting: true,
schemaVersion: SCHEMA_VERSION_THAT_DOESNT_INCLUDE_METADATA_ITSELF,
});
const cxasm = await cx.synthesize();

const result = cxasm.stackById('withouterrors').firstStack;
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
expect(metadata).toEqual({
Type: 'AWS::CDK::Metadata',
Properties: {
// eslint-disable-next-line @typescript-eslint/no-require-imports
Modules: `${require('../../package.json').name}=${require('../../package.json').version}`,
},
Condition: 'CDKMetadataAvailable',
});

expect(result.template.Conditions?.CDKMetadataAvailable).toBeDefined();
});

test('is generated for stacks in supported regions from old frameworks', async () => {
const cx = await testCloudExecutable({
env: 'aws://012345678912/us-east-1',
versionReporting: true,
schemaVersion: SCHEMA_VERSION_THAT_DOESNT_INCLUDE_METADATA_ITSELF,
});
const cxasm = await cx.synthesize();

const result = cxasm.stackById('withouterrors').firstStack;
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
expect(metadata).toEqual({
Type: 'AWS::CDK::Metadata',
Properties: {
// eslint-disable-next-line @typescript-eslint/no-require-imports
Modules: `${require('../../package.json').name}=${require('../../package.json').version}`,
},
});
});

test('is not generated for stacks in unsupported regions from old frameworks', async () => {
const cx = await testCloudExecutable({
env: 'aws://012345678912/bermuda-triangle-1337',
versionReporting: true,
schemaVersion: SCHEMA_VERSION_THAT_DOESNT_INCLUDE_METADATA_ITSELF,
});
const cxasm = await cx.synthesize();

const result = cxasm.stackById('withouterrors').firstStack;
const metadata = result.template.Resources && result.template.Resources.CDKMetadata;
expect(metadata).toBeUndefined();
});

test('is not generated for new frameworks', async () => {
const cx = await testCloudExecutable({
env: 'aws://012345678912/us-east-1',
Expand Down

0 comments on commit 9e01387

Please sign in to comment.