For anyone looking for a solution in Python
:
from aws_cdk import aws_lambda as lambda_
api_function = lambda_.Function(
self,
"lambda_func",
code=lambda_.Code.from_bucket(bucket=s3_bucket, key='lambda.zip'),
runtime=typing.cast(lambda_.Runtime, lambda_.Runtime.PYTHON_3_9), # Bug in CDK/PyLance; use cast to mitigate for now
timeout=Duration.seconds(900),
handler="rest_client.handler"
)
cfn_func = api_function.node.default_child
cfn_func.override_logical_id("NewLogicalId")
(Note: PyLance
throws the following error, but cdk ls
and cdk synth
work as expected:
override_logical_id: Unknown
Cannot access member "override_logical_id" for type "IConstruct"
Member "override_logical_id" is unknownPylancereportGeneralTypeIssues
"override_logical_id" is not a known member of "None"
Please note: It is generally not recommended to change logical IDs as changing the logical ID of a resource causes the resource to be replaced. The old resource will get deleted, and a new resource with the new logical ID gets created.
From the docs:
Avoid changing the logical ID of a resource after it has been created. Since AWS CloudFormation identifies resources by their logical ID, if you change the logical ID of a resource, AWS CloudFormation deletes the existing resource, and then creates a new resource with the new logical ID, which may cause service interruption or data loss.