如何使用localstack部署SAM堆栈? [英] How to deploy SAM stack with localstack?

查看:243
本文介绍了如何使用localstack部署SAM堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了SAM堆栈,并且可以在AWS上构建,打包和部署它.我也可以使用局部本地启动或调用进行测试.现在,我需要测试其他AWS资源.我已将这些资源添加到我的SAM模板中.它在AWS上运行良好,但我正在寻找一种在本地堆栈(例如本地dynamodb)中部署SAM堆栈的方法.现在,我必须使用CLI创建资源,然后可以部署SAM堆栈(在这种情况下,仅部署lambda,API GW).我该怎么办?

I've written a SAM stack and I can build, package and deploy it on AWS. I can also use start-local or invoke to test. Now I need to test to other AWS resources. I've added those resources to my SAM template. It works well on AWS but I'm searching for a way to deploy my SAM stack in localstack (local dynamodb e.g.). Now I have to create the resources with the CLI and after that I can deploy my SAM stack (only lambda, API GW in this case). How can I do this?

推荐答案

我刚刚经历了这个过程.我认为以下是正确的.

I've just gone through this. I think the following is correct.

AWS SAM是Cloudformation的包装器.因此,您的SAM模板实际上是Cloudformation模板.您的Cloudformation模板定义了lambda和dynamodb等.当您将所有lambda和dynamodb部署到AWS时,就可以在云中进行测试了.

AWS SAM is a wrapper round Cloudformation. So your SAM template is actually a Cloudformation template. Your Cloudformation template defines your lambdas and dynamodb etc. When you deploy to AWS all of your lambdas and dynamodb go into AWS and you can test in the cloud.

在本地运行AWS SAM时,在本地(在docker容器中)运行lambda,但它仍可访问AWS云中的资源.

When you run AWS SAM locally you run your lambda locally (in a docker container) but it still access resources in the AWS cloud.

LocalStack确实具有CloudFormation接口,因此应该可以部署CloudFormation模板文件.但是我遇到了一些问题并放弃了.

LocalStack does have a CloudFormation interface, so it should be possible to deploy your CloudFormation template file. But I ran into a few issues with this and gave up.

无服务器框架与AWS SAM相似,因为它是一个框架,用于开发您的无服务(lambda)代码并将其部署到AWS. Serverless有自己的yaml规范来定义堆栈.从Cloudformation转换为无服务器yaml有点工作.

Serverless Framework is similar to AWS SAM in that it is a framework to develop your serveless (lambda) code and to deploy that to AWS. Serverless has it's own yaml specification for defining a stack. Converting from Cloudformation to Serverless yaml is a little bit of work.

有一个用于本地堆栈的无服务器插件.然后可以将代码部署到localstack.如果您的lambda前面有API Gateway,那么您可以点击一个本地网址来触发您的lambda.这完全在localstack中,而不使用AWS SAM.

There is a serverless plugin for localstack. It is then possible to deploy your code to localstack. If you have API Gateway infront of your lambdas then there will be a local url you can hit which triggers your lambdas. This is fully within localstack and not using AWS SAM.

这时,您可能会发现您的代码仍然符合真正的AWS服务.因此,您需要将端点URL更改为本地指向,如Andrew A.所述.为此,为了使代码在测试和生产中保持一致,可以为每个服务端点使用环境变量.

At this point you may find your code still hits the real AWS services. So you need to change the endpoint-url to point locally, as Andrew A. mentioned. For this, and to keep your code the same for test and production you can use environment variables for each of the service endpoints.

正如安德鲁·A(Andrew A.)所提到的那样,应该可以使用SAM local运行代码,该SAM local访问localstack提供的资源.但是,如果是在测试管道中完成的话,最好保留一个工具.

As Andrew A. mentions it should be possible to run code using SAM local which accesses resources provided by localstack. However, it may be preferrable to keep to one tool, if this was done within a testing pipeline say.

这篇关于如何使用localstack部署SAM堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆