MikeBild / sveltekit-adapter-aws

A SvelteKit adapter for AWS using the AWS-CDK
MIT License
135 stars 33 forks source link

Enhance deployment guide for AWS lambda #4

Open souravjamwal77 opened 2 years ago

souravjamwal77 commented 2 years ago

Is your feature request related to a problem? Please describe. Yes, README.ms docs are not very clear about the steps after the build. I would like to have complete docs from git clone to deploy to AWS with API gateway or with custom domain.

Describe alternatives you've considered I tried the SvelteKit lambda adapter but couldn't get the simple lambda-based deployment. Serverless.js docs also doesn't cover the deployment for SvelteKit.

Additional context SvelteKit lambda adapter could be of help (Here), Steps after build are not clear.

MikeBild commented 2 years ago

Hey @souravjamwal77! Thank's for your report. Before rewriting the README.md, let's get closer to the issue.

I try to explain the procedure for a completely new SvelteKit project step by step:

  1. Init a SvelteKit project npm create svelte@latest my-sveltekit-project
  2. cd my-sveltekit-project
  3. npm install --save-dev sveltekit-adapter-aws
  4. Edit your svelte.config.js
    
    import { adapter } from 'sveltekit-adapter-aws';
    import preprocess from 'svelte-preprocess';

export default { preprocess: preprocess(), kit: { adapter: adapter({ autoDeploy: true, FQDN: 'my-name.my-route53-domain.com' }), }, };


5. Enter `npm run build` to start the SvelteKit build and deployment to your personal **default** AWS account configured in `~/.aws/config`

I hope that clarify the usage of this adapter. Please don't hesitate to ask further questions.
souravjamwal77 commented 2 years ago

How do we specify the aws --profile? Also, aws-cdk bootstrap option is not working well with your setup. After npm run build below are the errors I'm getting

Run npm run preview to preview your production build locally.

> Using adapter-awscdk

✨  Synthesis time: 2.61s

sveltekit-sourav-fun-aws-basic-demo: deploying...
current credentials could not be used to assume 'arn:aws:iam::962856495980:role/cdk-hnb659fds-deploy-role-962856495980-us-east-1', but are for the right account. Proceeding anyway.

 ❌  sveltekit-sourav-fun-aws-basic-demo failed: Error: sveltekit-sourav-fun-aws-basic-demo: SSM parameter /cdk-bootstrap/hnb659fds/version not found. Has the environment been bootstrapped? Please run 'cdk bootstrap' (see https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)
    at CloudFormationDeployments.validateBootstrapStackVersion (/home/souravjamwal77/Desktop/projects/sveltekit-adapter-aws-basic-example/node_modules/aws-cdk/lib/api/cloudformation-deployments.ts:487:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at CloudFormationDeployments.publishStackAssets (/home/souravjamwal77/Desktop/projects/sveltekit-adapter-aws-basic-example/node_modules/aws-cdk/lib/api/cloudformation-deployments.ts:462:7)
    at CloudFormationDeployments.deployStack (/home/souravjamwal77/Desktop/projects/sveltekit-adapter-aws-basic-example/node_modules/aws-cdk/lib/api/cloudformation-deployments.ts:343:7)
    at CdkToolkit.deploy (/home/souravjamwal77/Desktop/projects/sveltekit-adapter-aws-basic-example/node_modules/aws-cdk/lib/cdk-toolkit.ts:219:24)
    at initCommandLine (/home/souravjamwal77/Desktop/projects/sveltekit-adapter-aws-basic-example/node_modules/aws-cdk/lib/cli.ts:347:12)

sveltekit-sourav-fun-aws-basic-demo: SSM parameter /cdk-bootstrap/hnb659fds/version not found. Has the environment been bootstrapped? Please run 'cdk bootstrap' (see https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)
  ✔ done
MikeBild commented 2 years ago

Thank you. It seems to be your AWS-CDK is not well prepared for aws-cdk deployments. Just prepare your AWS account and region using AWS-CDK by entering the following command and try again yarn build:

cdk bootstrap aws://<your account numer>/<your region>

https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html

MikeBild commented 2 years ago

Any additional questions?

DefinitelyMaybe commented 2 years ago

Follow on question if I may,

Do previous builds AWS resources remain with each successive build? I'm new to AWS so this may be a dumb question.

MikeBild commented 2 years ago

There is no dumb question. ;-) Yes, the AWS resources persist with each subsequent build. Or rather, with every successful deployment after the Sveltekit build. To destroy all the created AWS resources, it is possible to add the following to your package.json.

(https://github.com/MikeBild/sveltekit-adapter-aws-advanced-example/blob/master/package.json#L15)

"script": {
  "destroy": "destroy"
}
bryantbiggs commented 2 years ago

apologies if this is the wrong thread but - would be possible to provide the existing resources for this adapter? For example - I have my S3 bucket, CloudFront distro, and even lambda@edge functions (not for SSR), and it looks like all I am missing to get SSR working is:

  1. API Gateway w/ lambda for rendering (which I can create through our normal infra creation means)
  2. Add that endpoint to my CloudFront origin
  3. Something with the adapter to tell it ???

Would that be possible or not really?

MikeBild commented 2 years ago

Theoretically, this would work with the AWS-CDK. But how and by whom are the used resources (copied files, etc.) updated and removed (destroy)?

tvanriper commented 1 year ago

FWIW, it took me a little bit to figure out about the need to run cdk bootstrap aws://<your account number>/<your region> so deployments would... deploy. Suggesting the possible need for that within this project's documentation could help. The errors messages received when you haven't done this do not provide much guidance (not the fault of this project, just how it happens when integrating between different technologies).

lukaskoeller commented 1 year ago

I'm currently also trial and error from error message to error message and already bootsraped the environment. No I'm stuck with the following errors during the deployment of assets.

current credentials could not be used to assume 'arn:aws:iam::***:role/cdk-hnb659fds-deploy-role-***-eu-central-1', but are for the right account. Proceeding anyway.
…
web:build: [0%] start: Publishing 861509f9607aa6bb9fd7034fb10fe6f0c60d91dfd7f010b9ef666869dec9bbd9:***-eu-central-1
…
web:build: current credentials could not be used to assume 'arn:aws:iam::***:role/cdk-hnb659fds-file-publishing-role-***-eu-central-1', but are for the right account. Proceeding anyway.
…
web:build: [11%] fail: Bucket named 'cdk-hnb659fds-assets-***-eu-central-1' exists, but not in account ***. Wrong account?
…
❌  *-app failed: Error: Failed to publish one or more assets. See the error messages above for more information.
…
❌ Deployment failed: Error: Stack Deployments Failed: Error: Failed to publish one or more assets. See the error messages above for more information.

Also this github job went through successfully although I'd expect it to fail. Can anybody help what I need to do to fix it?

lukaskoeller commented 1 year ago

Might be worth adding that I used Assume Role directly using GitHub OIDC provider (see Configure AWS Credentials for GitHub Actions) to setup credentials