Open hiddengearz opened 4 months ago
TIL you can import cloudformation into CDK, seems like this may be my best bet as I'm far more familiar with CDK.
Would still love to hear others opinions!
Importing the cloudformation CDK does work, though because the names of the constructs like S3 buckets and etc change everytime you'll have to play with regex if you need to make any modifications to them e.g assign roles.
It honestly may be easier to fork this repo and make changes to the original cdk in sveltekit-adapter-aws/cdk/arch
.
Here is an example if anyone decides to go down this route:
export class SveleteStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, {...props,analyticsReporting: false} );
const filePath = path.resolve(__dirname, '../../build/cdk.out/faulty-bedrock.template.json');
//Get template from Svelete build
const template = new cfninc.CfnInclude(this, 'Template', {
templateFile: filePath,
});
const [lambdaName, s3BucketName, cloudDistributionName] = getCFResources();
//get lambda that was genereated by Svelete
const cfnLambda = template.getResource(lambdaName) as lambda.CfnFunction;
const lambdaFunc = lambda.Function.fromFunctionName(this, lambdaName, cfnLambda.ref);
//create bedrock policy
const bedrockAccessPolicy = new PolicyStatement({
effect: Effect.ALLOW,
actions: ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"], // See: https://docs.aws.amazon.com/ja_jp/service-authorization/latest/reference/list_amazonbedrock.html
resources: ["arn:aws:bedrock:*::foundation-model/anthropic.claude-v2"],
});
//Add these policies to the role used by that lambda
lambdaFunc.addToRolePolicy(bedrockAccessPolicy);
Ultimately ended up just copying the cdk from the adapter and modifying it. The above method seems nice but is too much of a headache, likely caused some of the issues I experienced in #485
It is believed that only manual resource creation and integration is being used at the moment. However, integration with other AWS resources is an important issue. I would like to add an optional CDK extension function that takes the resource created by the adapter as an argument.
// svelte.config.js
{
// ...
adapter: adapter({
// ...
architecture: 'lambda-s3',
extends: (lambda, s3, cloudfront) => {
new aws.cognito.UserPoolClient(this, 'UserPoolClient', {
userPool: 'UserPool',
generateSecret: false,
userPoolClientName: 'UserPoolClient'
// ...
})
// ...
}
})
}
Naturally, they should be separated into separate files. We will also provide the necessary types.
// svelte.config.js
import { extendsCDK } from './extendsCDK.js'
{
// ...
adapter: adapter({
// ...
architecture: 'lambda-s3',
extends: extendsCDK
})
}
// extendsCDK.ts
import type { ExtendsLambdaS3 } from '@jill64/sveltekit-adapter-aws/types'
export const extendsCDK: ExtendsLambdaS3 = (lambda, s3, cloudfront) => {
new aws.cognito.UserPoolClient(this, 'UserPoolClient', {
userPool: 'UserPool',
generateSecret: false,
userPoolClientName: 'UserPoolClient'
// ...
})
// ...
}
I have considered merging CloudFormation templates, but it seems a bit too challenging for me. What do you think about this idea?
I think the above would work well, especially for simple stacks.
The main issue I ran into (which I believe this method will run into aswell) is that it seems to be far more difficult to make modifications to a stack/cloud formation template, from another stack. I had to make changes to the constructs in the generated stack and trying to do that from another stack, was annoying and ultimately didn't work. I'm not a pro but that is my experience from my attempt above.
For advanced stacks If I was able to modify the generated stack and it not be overwritten every time I run pnpm run build
that would be sufficient.
I'll upload the two CDK's I made as an example, both should of done basically the same thing (sorry for the messy code, I wasn't planning to post this publicly anytime soon)
In hindsight for attempt 1 I'm pretty sure I was using the wrong names to retrieve the constructs from the cloudformation template, so there would be no need for findResourceName
and it probably would of worked... but I think this shows the difference between the approaches.
I think you would want to extend via CDK. Have some way to allow the adapter to inject other stacks / files into the one cdk application or maybe allow specification of an alternative cdk app file that can import the Svelte Stack
Also there are easy ways to share resources across stacks, you can export and import resources from separate stacks using CfnOutput and Fn.ImportValue. If you haven't tried this before this works pretty well except for:
cdk deploy can output json with output values for use in other build process.
Hello,
Svelte & cloudformation noob here, I've been working on an demo app that uses AWS bedrock and cognito. However after using this adapter I realized it generates a cloudformation template and not the CDK code, so I'm a bit stuck on the best way to incorporate bedrock and cognito.
What would you suggest?