在lambda执行中运行CDK



我必须经常更新CloudWatch Dashboards

我正试图从lambda执行中运行CDK以获得Cloudformation模板。

执行构造函数app = cdk.App(outdir="./tmp")时抛出异常

[ERROR] FileNotFoundError: [Errno 2] No such file or directory: 'node'
Traceback (most recent call last):
  File "/var/lang/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/var/task/app.py", line 2, in <module>
    import aws_cdk as cdk
  File "/var/task/aws_cdk/__init__.py", line 1440, in <module>
    from ._jsii import *
  File "/var/task/aws_cdk/_jsii/__init__.py", line 13, in <module>
    import aws_cdk.asset_awscli_v1._jsii
  File "/var/task/aws_cdk/asset_awscli_v1/_jsii/__init__.py", line 13, in <module>
    __jsii_assembly__ = jsii.JSIIAssembly.load(
  File "/var/task/jsii/_runtime.py", line 55, in load
    _kernel.load(assembly.name, assembly.version, os.fspath(assembly_path))
  File "/var/task/jsii/_kernel/__init__.py", line 299, in load
    self.provider.load(LoadRequest(name=name, version=version, tarball=tarball))
  File "/var/task/jsii/_kernel/providers/process.py", line 352, in load
    return self._process.send(request, LoadResponse)
  File "/var/task/jsii/_utils.py", line 23, in wrapped
    stored.append(fgetter(self))
  File "/var/task/jsii/_kernel/providers/process.py", line 347, in _process
    process.start()
  File "/var/task/jsii/_kernel/providers/process.py", line 260, in start
    self._process = subprocess.Popen(
  File "/var/lang/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/var/lang/lib/python3.9/subprocess.py", line 1821, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)Exception ignored in: <function _NodeProcess.__del__ at 0x7f80ae481e50>
Traceback (most recent call last):
File "/var/task/jsii/_kernel/providers/process.py", line 228, in __del__
self.stop()
File "/var/task/jsii/_kernel/providers/process.py", line 291, in stop
assert self._process.stdin is not None
AttributeError: '_NodeProcess' object has no attribute '_process'

是否有办法从lambda执行运行CDK ?

我可以使用lambda和docker映像来实现这一点。

这是Dockerfile

FROM public.ecr.aws/lambda/python:3.9
# Install the function's dependencies using file requirements.txt
# from your project folder.
RUN yum update -y
RUN yum install -y curl
RUN curl -sL https://rpm.nodesource.com/setup_16.x | bash -

RUN yum install -y nodejs
RUN npm install -g aws-cdk
COPY requirements.txt  .
RUN  pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "app.lambda_handler" ]

下面是lambda代码示例

import json
import aws_cdk as cdk
import boto3
from aws_cdk import (
Duration,
Stack,
aws_sqs as sqs,
)
from constructs import Construct
class MonitorPocPythonStack(Stack):
def __init__(self, scope: Construct, construct_id: str, queue_name: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# The code that defines your stack goes here
# example resource
queue = sqs.Queue(
self, queue_name,
visibility_timeout=Duration.seconds(300),
)
def lambda_handler(event, context):
print("new 3")
cloudformation = boto3.client('cloudformation')
app = cdk.App(outdir="/tmp")
MonitorPocPythonStack(app, "MonitorPocPythonStack", event['queue'])
app.synth()

with open("/tmp/MonitorPocPythonStack.template.json", "r") as file:
template_body = file.read()

cloudformation.update_stack(
StackName="MonitorPocPythonStack",
TemplateBody=template_body,
Capabilities=[
'CAPABILITY_IAM','CAPABILITY_AUTO_EXPAND'
],
)
return {
"statusCode": 200,
"body": json.dumps({
"message": "hello world",
# "location": ip.text.replace("n", "")
}),
}

相关内容

  • 没有找到相关文章

最新更新