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

Fix the failure to deploy the edge lambda to US-East-1 #251

Merged
merged 7 commits into from
Oct 2, 2022
Merged
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ jobs:
run: yarn install --frozen-lockfile

- name: Build All TypeScript
run: yarn build
run: yarn build

- name: Build Edge-to-Origin for Local Deploy
run: yarn esbuild:edge-to-origin

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16.15.0
v16.17.1
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@pwrdrvr/microapps-core",
"resolutions": {
"constructs": "10.0.5"
"constructs": "10.0.5",
"terser": "^5.14.2"
},
"version": "0.0.0",
"repository": {
Expand All @@ -21,7 +22,8 @@
},
"private": true,
"scripts": {
"clean": "npm run clean:dist && npm run clean:modules && npm run clean:tsbuildinfo",
"clean": "npm run clean:dist && npm run clean:tsbuildinfo",
"clean:super": "npm run clean:dist && npm run clean:modules && npm run clean:tsbuildinfo",
"clean:dist": "npm exec --workspaces -- npx rimraf dist && npx rimraf dist",
"clean:modules": "npm exec --workspaces -- npx rimraf node_modules && npx rimraf node_modules",
"clean:tsbuildinfo": "npm exec --workspaces -- npx rimraf *.tsbuildinfo",
Expand All @@ -33,7 +35,7 @@
"build:publish": "tsc --build tsconfig.publish.json",
"esbuild:deployer": "esbuild packages/microapps-deployer/src/index.ts --bundle --minify --sourcemap --platform=node --target=node14 --external:aws-sdk --outfile=packages/cdk/dist/microapps-deployer/index.js",
"esbuild:router": "esbuild packages/microapps-router/src/index.ts --bundle --minify --sourcemap --platform=node --target=node14 --external:aws-sdk --outfile=packages/cdk/dist/microapps-router/index.js",
"esbuild:edge-to-origin": "esbuild packages/microapps-edge-to-origin/src/index.ts --bundle --minify --sourcemap --platform=node --target=node14 --external:aws-sdk --outfile=packages/microapps-edge-to-origin/dist/index.js",
"esbuild:edge-to-origin": "esbuild packages/microapps-edge-to-origin/src/index.ts --bundle --minify --sourcemap --platform=node --target=node16 --external:aws-sdk --outfile=packages/microapps-cdk/lib/microapps-edge-to-origin/index.js",
"build:deployer": "rollup --config rollup.deployer.js",
"build:router": "rollup --config rollup.router.js && cp packages/microapps-router/appFrame.html distb/microapps-router/",
"test": "AWS_PROFILE= AWS_EMF_ENVIRONMENT=Local jest",
Expand Down
82 changes: 49 additions & 33 deletions packages/microapps-cdk/src/MicroAppsEdgeToOrigin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ replaceHostHeader: ${props.replaceHostHeader}`;
functionName: assetNameRoot ? `${assetNameRoot}-edge-to-origin${assetNameSuffix}` : undefined,
memorySize: 1769,
logRetention: logs.RetentionDays.ONE_MONTH,
runtime: lambda.Runtime.NODEJS_14_X,
runtime: lambda.Runtime.NODEJS_16_X,
timeout: Duration.seconds(5),
initialPolicy: [
// This can't have a reference to the httpApi because it would mean
Expand All @@ -189,42 +189,40 @@ replaceHostHeader: ${props.replaceHostHeader}`;
],
...(removalPolicy ? { removalPolicy } : {}),
};
if (
process.env.NODE_ENV === 'test' &&
existsSync(path.join(__dirname, '..', '..', 'microapps-edge-to-origin', 'dist', 'index.js'))
) {
// Emit the config file from the construct options
writeFileSync(
path.join(__dirname, '..', '..', 'microapps-edge-to-origin', 'dist', 'config.yml'),
const rootDistPath = path.join(__dirname, '..', '..', 'microapps-edge-to-origin', 'dist');
const rootDistExists = existsSync(path.join(rootDistPath, 'index.js'));
const localDistPath = path.join(__dirname, 'microapps-edge-to-origin');
const localDistExists = existsSync(path.join(localDistPath, 'index.js'));
if (process.env.NODE_ENV === 'test' && rootDistExists) {
// This is for tests run under jest - Prefer root dist bundle
// This is also for anytime when the edge function has already been bundled
this._edgeToOriginFunction = this.createEdgeFunction(
rootDistPath,
edgeToOriginConfigYaml,
edgeToOriginFuncProps,
);
// copyFileSync(
// path.join(__dirname, '..', '..', '..', 'configs', 'microapps-edge-to-origin', 'config.yml'),
// path.join(__dirname, '..', '..', 'microapps-edge-to-origin', 'dist', 'config.yml'),
// );
// This is for tests run under jest
// This is also for anytime when the edge function has already been bundled
this._edgeToOriginFunction = new cf.experimental.EdgeFunction(this, 'edge-to-apigwy-func', {
code: lambda.Code.fromAsset(
path.join(__dirname, '..', '..', 'microapps-edge-to-origin', 'dist'),
),
handler: 'index.handler',
...edgeToOriginFuncProps,
});
} else if (existsSync(path.join(__dirname, 'microapps-edge-to-origin', 'index.js'))) {
// Emit the config file from the construct options
writeFileSync(
path.join(__dirname, 'microapps-edge-to-origin', 'config.yml'),
} else if (localDistExists) {
// Prefer local dist above root dist if both exist (when building for distribution)
this._edgeToOriginFunction = this.createEdgeFunction(
localDistPath,
edgeToOriginConfigYaml,
edgeToOriginFuncProps,
);
} else if (rootDistExists) {
// Use local dist if it exists (when deploying from CDK in this repo)
this._edgeToOriginFunction = this.createEdgeFunction(
rootDistPath,
edgeToOriginConfigYaml,
edgeToOriginFuncProps,
);

// This is for built apps packaged with the CDK construct
this._edgeToOriginFunction = new cf.experimental.EdgeFunction(this, 'edge-to-apigwy-func', {
code: lambda.Code.fromAsset(path.join(__dirname, 'microapps-edge-to-origin')),
handler: 'index.handler',
...edgeToOriginFuncProps,
});
} else {
// 2022-07-30 - Does this actually get used at all anymore?

// 2022-10-02 - This is broken - it's emitting a config file but then
// usinga different config file in the bundling below.
// This may be ok if this is only used for the construct packaging
// as the consuming stack should select a different above which will
// use the correct config file.
// Emit the config file from the construct options
writeFileSync(
path.join(__dirname, '..', '..', 'microapps-edge-to-origin', 'config.yml'),
Expand All @@ -235,7 +233,6 @@ replaceHostHeader: ${props.replaceHostHeader}`;
// and will be used during local builds and PR builds of microapps-core
// if the microapps-edge-to-origin function is not already bundled.
// This will fail to deploy in any region other than us-east-1
// We cannot use NodejsFunction because it will not create in us-east-1
this._edgeToOriginFunction = new lambdaNodejs.NodejsFunction(this, 'edge-to-apigwy-func', {
entry: path.join(__dirname, '..', '..', 'microapps-edge-to-origin', 'src', 'index.ts'),
handler: 'handler',
Expand All @@ -246,6 +243,8 @@ replaceHostHeader: ${props.replaceHostHeader}`;
beforeInstall: () => [],
beforeBundling: () => [],
afterBundling: (_inputDir: string, outputDir: string) => {
// 2022-10-02 - Note that this is ignoring the generated config
// file above and including the default template config file
return [
`${os.platform() === 'win32' ? 'copy' : 'cp'} ${path.join(
__dirname,
Expand All @@ -272,4 +271,21 @@ replaceHostHeader: ${props.replaceHostHeader}`;
},
];
}

private createEdgeFunction(
distPath: string,
edgeToOriginConfigYaml: string,
edgeToOriginFuncProps: Omit<lambda.FunctionProps, 'handler' | 'code'>,
) {
writeFileSync(path.join(distPath, 'config.yml'), edgeToOriginConfigYaml);

return new cf.experimental.EdgeFunction(this, 'edge-to-apigwy-func', {
code: lambda.Code.fromAsset(distPath),
handler: 'index.handler',
...(edgeToOriginFuncProps.functionName
? { stackId: edgeToOriginFuncProps.functionName }
: {}),
...edgeToOriginFuncProps,
});
}
}
Loading