Given a bucket and key (or a local path) of a valid Lambda-zip, a function name and a handler, update that Lambda function's code. Optionally, delete previous versions if they exceed the optional limit.
(
self,
bucket,
function_name,
s3_key=None,
publish=True,
local_zip=None,
num_revisions=None,
concurrency=None,
docker_image_uri=None,
)
| 1278 | return resource_arn |
| 1279 | |
| 1280 | def update_lambda_function( |
| 1281 | self, |
| 1282 | bucket, |
| 1283 | function_name, |
| 1284 | s3_key=None, |
| 1285 | publish=True, |
| 1286 | local_zip=None, |
| 1287 | num_revisions=None, |
| 1288 | concurrency=None, |
| 1289 | docker_image_uri=None, |
| 1290 | ): |
| 1291 | """ |
| 1292 | Given a bucket and key (or a local path) of a valid Lambda-zip, |
| 1293 | a function name and a handler, update that Lambda function's code. |
| 1294 | Optionally, delete previous versions if they exceed the optional limit. |
| 1295 | """ |
| 1296 | logger.info("Updating Lambda function code..") |
| 1297 | |
| 1298 | kwargs = dict(FunctionName=function_name, Publish=publish) |
| 1299 | if docker_image_uri: |
| 1300 | kwargs["ImageUri"] = docker_image_uri |
| 1301 | elif local_zip: |
| 1302 | kwargs["ZipFile"] = local_zip |
| 1303 | else: |
| 1304 | kwargs["S3Bucket"] = bucket |
| 1305 | kwargs["S3Key"] = s3_key |
| 1306 | |
| 1307 | response = self.lambda_client.update_function_code(**kwargs) |
| 1308 | resource_arn = response["FunctionArn"] |
| 1309 | version = response["Version"] |
| 1310 | |
| 1311 | # If the lambda has an ALB alias, let's update the alias |
| 1312 | # to point to the newest version of the function. We have to use a GET |
| 1313 | # here, as there's no HEAD-esque call to retrieve metadata about a |
| 1314 | # function alias. |
| 1315 | # Related: https://github.com/Miserlou/Zappa/pull/1730 |
| 1316 | # https://github.com/Miserlou/Zappa/issues/1823 |
| 1317 | try: |
| 1318 | response = self.lambda_client.get_alias( |
| 1319 | FunctionName=function_name, |
| 1320 | Name=ALB_LAMBDA_ALIAS, |
| 1321 | ) |
| 1322 | alias_exists = True |
| 1323 | except botocore.exceptions.ClientError as e: # pragma: no cover |
| 1324 | if "ResourceNotFoundException" not in e.response["Error"]["Code"]: |
| 1325 | raise e |
| 1326 | alias_exists = False |
| 1327 | |
| 1328 | if alias_exists: |
| 1329 | self.lambda_client.update_alias( |
| 1330 | FunctionName=function_name, |
| 1331 | FunctionVersion=version, |
| 1332 | Name=ALB_LAMBDA_ALIAS, |
| 1333 | ) |
| 1334 | |
| 1335 | if concurrency is not None: |
| 1336 | self.lambda_client.put_function_concurrency( |
| 1337 | FunctionName=function_name, |