简体   繁体   English

AWS lambda:如何在 lambda 中运行 aws cli 命令

[英]AWS lambda: how can I run aws cli commands in lambda

I want to run aws cli commands from lambda我想从 lambda 运行 aws cli 命令

I have a Pull request event that triggers when the approval state changes and whenever it's changed I need to run an aws CLI command from lambda but the lambda function says aws not found?我有一个拉取请求事件,当批准 state 更改时触发,每当它更改时,我需要从 lambda 运行 aws CLI 命令,但 lambda function 说找不到 aws? how do I get the status on PR's in my lambda function?如何在我的 lambda 函数中获取 PR 的状态?

Create a lambda function, build an image to ecr, have the lambda function reference the image, and then test the image with an event.创建一个 lambda function,将图像构建到 ecr,让 lambda function 引用图像,然后使用事件测试图像。 This is a good way to run things like aws s3 sync .这是运行aws s3 sync类的东西的好方法。

Testing local:本地测试:

docker run -p 9000:8080 repo/lambda:latest
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

app.py应用程序.py

import subprocess
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)


def run_command(command):
    try:
        logger.info('Running shell command: "{}"'.format(command))
        result = subprocess.run(command, stdout=subprocess.PIPE, shell=True)
        logger.info(
            "Command output:\n---\n{}\n---".format(result.stdout.decode("UTF-8"))
        )
    except Exception as e:
        logger.error("Exception: {}".format(e))
        return False

    return True


def handler(event, context):
    run_command('aws s3 ls')

Dockerfile (awscliv2, can make requirements file if needed) Dockerfile(awscliv2,如果需要可以制作需求文件)

FROM public.ecr.aws/lambda/python:3.9

RUN yum -y install unzip

RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64-2.0.30.zip" -o "awscliv2.zip" && \
  unzip awscliv2.zip && \
  ./aws/install

COPY app.py ${LAMBDA_TASK_ROOT}

COPY requirements.txt  .
RUN  pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"

CMD [ "app.handler" ]

Makefile (make all - login,build,tag,push to ecr repo) Makefile (make all - login,build,tag,push to ecr repo)

ROOT:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
IMAGE_NAME:=repo/lambda

ECR_TAG:="latest"
AWS_REGION:="us-east-1"
AWS_ACCOUNT_ID:="xxxxxxxxx"
REGISTRY_URI=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME}
REGISTRY_URI_WITH_TAG=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME}:${ECR_TAG}

# Login to AWS ECR registry (must have docker running)
login:
    aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${REGISTRY_URI}

build:
    docker build --no-cache -t ${IMAGE_NAME}:${ECR_TAG} .

# Tag docker image
tag:
    docker tag ${IMAGE_NAME}:${ECR_TAG} ${REGISTRY_URI_WITH_TAG}

# Push to ECR registry
push:
    docker push ${REGISTRY_URI_WITH_TAG}

# Pull version from ECR registry
pull:
    docker pull ${REGISTRY_URI_WITH_TAG}

# Build docker image and push to AWS ECR registry
all: login build tag push

The default lambda environment doesn't provide the awscli.默认的 lambda 环境不提供 awscli。 In fact, the idea of using it there is quite awkward.事实上,在那里使用它的想法是相当别扭的。 You can call any command the aws cli can via an sdk like boto3 for example, which is provided in that environment.例如,您可以通过该环境中提供的 sdk(例如boto3)调用 aws cli 可以调用的任何命令。

You can however include binaries in your lambda, if you please, then execute them.但是,如果愿意,您可以在 lambda 中包含二进制文件,然后执行它们。

You also consider using a container image for your lambda.您还可以考虑为 lambda 使用容器映像。 You can find information here:https://docs.aws.amazon.com/lambda/latest/dg/images-create.html .您可以在此处找到信息:https ://docs.aws.amazon.com/lambda/latest/dg/images-create.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM