我在python3.9上构建了一个HelloWorld模板,并使用AWS将其打包为Docker。
除了sam sync --watch --stack-name hello-world之外,每个SAM命令都运行良好,这会导致以下错误:
samcli.commands.package.exceptions.ExportFailedError: Unable to upload artifact helloworldfunction:python3.9-v2 referenced by ImageUri parameter of HelloWorldFunction resource.在部署时,甚至在使用sam sync标志运行--code时,我都可以将映像上传到ECR中。我试过使用--resource-id标志,但没有效果。
我不知道这里发生了什么..。帮助!
这是我的Dockerfile:
FROM public.ecr.aws/lambda/python:3.9
COPY app.py requirements.txt ./
RUN python3.9 -m pip install -r requirements.txt -t .
# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]这是我的构建template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'python3.9
Sample SAM Template for hello-world
'
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
PackageType: Image
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
ImageUri: helloworldfunction:python3.9-v2
Metadata:
DockerContext: /Users/nelson/Applications/hello-world/hello_world
DockerTag: python3.9-v2
Dockerfile: Dockerfile
SamResourceId: HelloWorldFunction
Outputs:
HelloWorldApi:
Description: API Gateway endpoint URL for Prod stage for Hello World function
Value:
Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/
HelloWorldFunction:
Description: Hello World Lambda Function ARN
Value:
Fn::GetAtt:
- HelloWorldFunction
- Arn
HelloWorldFunctionIamRole:
Description: Implicit IAM Role created for Hello World function
Value:
Fn::GetAtt:
- HelloWorldFunctionRole
- Arn下面是运行调试(sam sync --watch --stack-name hello-world --debug)时的跟踪:
2022-07-11 23:46:40,776 | Building image for HelloWorldFunction function
2022-07-11 23:46:40,780 | Setting DockerBuildArgs: {} for HelloWorldFunction function
Step 1/4 : FROM public.ecr.aws/lambda/python:3.9
---> a0bdfefcccf7
Step 2/4 : COPY app.py requirements.txt ./
---> Using cache
---> 8437c09ef028
Step 3/4 : RUN python3.9 -m pip install -r requirements.txt -t .
---> Using cache
---> 38afa314fe71
Step 4/4 : CMD ["app.lambda_handler"]
---> Using cache
---> af4fc1a40809
Successfully built af4fc1a40809
Successfully tagged helloworldfunction:python3.9-v2
2022-07-11 23:46:40,878 | Async execution completed
2022-07-11 23:46:40,879 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-07-11 23:46:40,880 | Auto creating dependency layer for each function resource into a nested stack
2022-07-11 23:46:40,880 | 2 resources found in the stack
2022-07-11 23:46:40,880 | Found Serverless function with name='HelloWorldFunction' and ImageUri='None'
2022-07-11 23:46:40,880 | --base-dir is not presented, adjusting uri /Users/nelson/Applications/hello-world/hello_world relative to /Users/nelson/Applications/hello-world/template.yaml
2022-07-11 23:46:40,880 | --base-dir is not presented, adjusting uri . relative to /Users/nelson/Applications/hello-world/template.yaml
2022-07-11 23:46:40,880 | No function has been added for auto dependency layer creation
Build Succeeded
2022-07-11 23:46:40,888 | No Parameters detected in the template
2022-07-11 23:46:40,900 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction is HelloWorldFunction
2022-07-11 23:46:40,900 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-07-11 23:46:40,900 | 0 stacks found in the template
2022-07-11 23:46:40,950 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction is HelloWorldFunction
2022-07-11 23:46:40,950 | Sam customer defined id is more priority than other IDs. Customer defined id for resource HelloWorldFunction is HelloWorldFunction
2022-07-11 23:46:41,417 | Unable to export
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/api/client.py", line 261, in _raise_for_status
response.raise_for_status()
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/requests/models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http+docker://localhost/v1.35/images/sha256:af4fc1a40809d6386a8fc05e05bcc6bfcaa9c0fc353028dddf6b372210e8c676/tag?tag=helloworldfunction-af4fc1a40809-python3.9-v2&force=0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/ecr_uploader.py", line 88, in upload
docker_img.tag(repository=repository, tag=_tag)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/models/images.py", line 122, in tag
return self.client.api.tag(self.id, repository, tag=tag, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/utils/decorators.py", line 19, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/api/image.py", line 547, in tag
self._raise_for_status(res)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/api/client.py", line 263, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 500 Server Error: Internal Server Error ("invalid reference format")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/packageable_resources.py", line 273, in export
self.do_export(resource_id, resource_dict, parent_dir)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/packageable_resources.py", line 286, in do_export
uploaded_url = upload_local_image_artifacts(
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/utils.py", line 118, in upload_local_image_artifacts
return uploader.upload(image_path, resource_id)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/ecr_uploader.py", line 101, in upload
raise DockerPushFailedError(msg=str(ex)) from ex
samcli.commands.package.exceptions.DockerPushFailedError: 500 Server Error: Internal Server Error ("invalid reference format")
2022-07-11 23:46:41,419 | Failed to sync infra. Code sync is paused until template/stack is fixed.
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/api/client.py", line 261, in _raise_for_status
response.raise_for_status()
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/requests/models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http+docker://localhost/v1.35/images/sha256:af4fc1a40809d6386a8fc05e05bcc6bfcaa9c0fc353028dddf6b372210e8c676/tag?tag=helloworldfunction-af4fc1a40809-python3.9-v2&force=0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/ecr_uploader.py", line 88, in upload
docker_img.tag(repository=repository, tag=_tag)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/models/images.py", line 122, in tag
return self.client.api.tag(self.id, repository, tag=tag, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/utils/decorators.py", line 19, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/api/image.py", line 547, in tag
self._raise_for_status(res)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/api/client.py", line 263, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 500 Server Error: Internal Server Error ("invalid reference format")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/packageable_resources.py", line 273, in export
self.do_export(resource_id, resource_dict, parent_dir)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/packageable_resources.py", line 286, in do_export
uploaded_url = upload_local_image_artifacts(
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/utils.py", line 118, in upload_local_image_artifacts
return uploader.upload(image_path, resource_id)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/ecr_uploader.py", line 101, in upload
raise DockerPushFailedError(msg=str(ex)) from ex
samcli.commands.package.exceptions.DockerPushFailedError: 500 Server Error: Internal Server Error ("invalid reference format")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/watch_manager.py", line 190, in _execute_infra_sync
self._execute_infra_context()
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/sync/watch_manager.py", line 143, in _execute_infra_context
self._package_context.run()
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/commands/package/package_context.py", line 138, in run
exported_str = self._export(self.template_file, self.use_json)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/commands/package/package_context.py", line 160, in _export
exported_template = template.export()
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/artifact_exporter.py", line 293, in export
exporter.export(full_path, resource_dict, self.template_dir)
File "/usr/local/Cellar/aws-sam-cli/1.53.0/libexec/lib/python3.8/site-packages/samcli/lib/package/packageable_resources.py", line 277, in export
raise exceptions.ExportFailedError(
samcli.commands.package.exceptions.ExportFailedError: Unable to upload artifact helloworldfunction:python3.9-v2 referenced by ImageUri parameter of HelloWorldFunction resource.
500 Server Error: Internal Server Error ("invalid reference format")
2022-07-11 23:46:41,424 | No Parameters detected in the template
2022-07-11 23:46:41,436 | There is no customer defined id or cdk path defined for resource HelloWorldFunction, so we will use the resource logical id as the resource id
2022-07-11 23:46:41,436 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2022-07-11 23:46:41,436 | 0 stacks found in the template
^C2022-07-11 23:46:48,171 | Shutting down sync watch...
2022-07-11 23:46:48,172 | Sync watch stopped.为什么我只根据这个命令得到了这个错误?!
发布于 2022-07-19 03:57:33
至少有两个解决方案。
可以使用--image-repository或--image-repositories标志运行相同的命令。例如:
sam sync --watch --stack-name hello-world --image-repository <account_id>.dkr.ecr.<region>.amazonaws.com/<repository-name>或者,您可以编辑项目级配置文件(.toml),并将映像存储库(Ies)URI保存为sync命令下的参数。我在名为samconfig.toml的文件中添加了以下行
[default.sync.parameters]
image_repositories = ["HelloWorldFunction=<account_id>.dkr.ecr.<region>.amazonaws.com/<repository-name>"]为了找到映像存储库URI(Ies),您必须已经部署了应用程序。否则,您将没有将您的形象推到ECR,在第一。
之后,
[default.deploy.parameters]aws cloudformation describe-stack-resources --stack-name <stack-name>老实说,虽然我修复了错误,但我不明白为什么这些更改是必要的。为什么每次部署时sync文件中的部署参数没有被复制到.toml文件中?如有任何意见,将不胜感激。
https://stackoverflow.com/questions/72946880
复制相似问题