运行
get-login-password
命令,以针对 Amazon ECR 注册表进行 Docker CLI 身份验证。
将
111122223333
替换为您的 AWS 账户 ID。
aws ecr get-login-password --region us-east-1
| docker login --username AWS --password-stdin 111122223333
.dkr.ecr.us-east-1
.amazonaws.com
使用
create-repository
命令在 Amazon ECR 中创建存储库。
aws ecr create-repository --repository-name hello-world
--region us-east-1
--image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
Amazon ECR 存储库必须与 Lambda 函数位于同一 AWS 区域 内。
如果成功,您将会看到如下响应:
"repository":
{
"repositoryArn": "arn:aws:ecr:us-east-1:111122223333:repository/hello-world",
"registryId": "111122223333",
"repositoryName": "hello-world",
"repositoryUri": "111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world",
"createdAt": "2023-03-09T10:39:01+00:00",
"imageTagMutability": "MUTABLE",
"imageScanningConfiguration":
{
"scanOnPush": true
"encryptionConfiguration":
{
"encryptionType": "AES256"
从上一步的输出中复制
repositoryUri
。
运行
docker tag
命令,将本地映像作为最新版本标记到 Amazon ECR 存储库中。在此命令中:
docker-image:test
是 Docker 映像的名称和
标签
。这是您在
docker build
命令中指定的映像名称和标签。
将
<ECRrepositoryUri>
替换为复制的
repositoryUri
。确保 URI 末尾包含
:latest
。
docker tag docker-image:test <ECRrepositoryUri>
:latest
docker tag docker-image
:test
111122223333
.dkr.ecr.us-east-1
.amazonaws.com/hello-world
:latest
运行
docker push
命令,以将本地映像部署到 Amazon ECR 存储库。确保存储库 URI 末尾包含
:latest
。
docker push 111122223333
.dkr.ecr.us-east-1
.amazonaws.com/hello-world
:latest
如果您还没有函数的执行角色,请
创建执行角色
。在下一步中,您需要提供角色的 Amazon 资源名称(ARN)。
创建 Lambda 函数。对于
ImageUri
,指定之前的存储库 URI。确保 URI 末尾包含
:latest
。
aws lambda create-function \
--function-name hello-world
\
--package-type Image \
--code ImageUri=111122223333
.dkr.ecr.us-east-1
.amazonaws.com/hello-world
:latest \
--role arn:aws:iam::111122223333:role/lambda-ex
调用函数。
aws lambda invoke --function-name hello-world
response.json
应出现如下响应:
"ExecutedVersion": "$LATEST",
"StatusCode": 200
要查看函数的输出,请检查
response.json
文件。