Skip to content

update_function_code

update_function_code(
    function_name: FunctionName,
    *,
    config_overrides: Optional[LambdaClientConfig] = None,
    zip_file: Optional[Blob] = None,
    s3_bucket: Optional[S3Bucket] = None,
    s3_key: Optional[S3Key] = None,
    s3_object_version: Optional[S3ObjectVersion] = None,
    s3_object_storage_mode: Optional[S3ObjectStorageMode] = None,
    image_uri: Optional[String] = None,
    architectures: Optional[ArchitecturesList] = None,
    publish: Optional[Boolean] = None,
    publish_to: Optional[FunctionVersionLatestPublished] = None,
    dry_run: Optional[Boolean] = None,
    revision_id: Optional[String] = None,
    source_kms_key_arn: Optional[KMSKeyArn] = None,
) -> FunctionConfiguration

Updates a Lambda Event Function's code. If code signing is enabled for the function, the code package must be signed by a trusted publisher. For more information, see Configuring code signing for Lambda.

If the function's package type is Image, then you must specify the code package in ImageUri as the URI of a container image in the Amazon ECR registry.

If the function's package type is Zip, then you must specify the deployment package as a .zip file archive. Enter the Amazon S3 bucket and key of the code .zip file location. You can also provide the function code inline using the ZipFile field.

The code in the deployment package must be compatible with the target instruction set architecture of the function (x86-64 or arm64).

The function's code is locked when you publish a version. You can't modify the code of a published version, only the unpublished version.

For a function defined as a container image, Lambda resolves the image tag to an image digest. In Amazon ECR, if you update the image tag to a new image, Lambda does not automatically update the function.

Parameters:

  • function_name (FunctionName) –

    The name or ARN of the Lambda function.

    Name formats

    • Function name – my-function.

    • Function ARN – arn:aws:lambda:us-west-2:123456789012:function:my-function.

    • Partial ARN – 123456789012:function:my-function.

    The length constraint applies only to the full ARN. If you specify only the function name, it is limited to 64 characters in length.

  • zip_file (Optional[Blob], default: None ) –

    The base64-encoded contents of the deployment package. Amazon Web Services SDK and CLI clients handle the encoding for you. Use only with a function defined with a .zip file archive deployment package.

  • s3_bucket (Optional[S3Bucket], default: None ) –

    An Amazon S3 bucket in the same Amazon Web Services Region as your function. The bucket can be in a different Amazon Web Services account. Use only with a function defined with a .zip file archive deployment package.

  • s3_key (Optional[S3Key], default: None ) –

    The Amazon S3 key of the deployment package. Use only with a function defined with a .zip file archive deployment package.

  • s3_object_version (Optional[S3ObjectVersion], default: None ) –

    For versioned objects, the version of the deployment package object to use.

  • s3_object_storage_mode (Optional[S3ObjectStorageMode], default: None ) –

    Specifies how the deployment package is stored. Valid values:

    • COPY (default) – Uploads a copy of your deployment package to Lambda.

    • REFERENCE – Lambda references the deployment package from the specified Amazon S3 bucket.

  • image_uri (Optional[String], default: None ) –

    URI of a container image in the Amazon ECR registry. Do not use for a function defined with a .zip file archive.

  • architectures (Optional[ArchitecturesList], default: None ) –

    The instruction set architecture that the function supports. Enter a string array with one of the valid values (arm64 or x86_64). The default value is x86_64.

  • publish (Optional[Boolean], default: None ) –

    Set to true to publish a new version of the function after updating the code. This has the same effect as calling PublishVersion separately.

  • publish_to (Optional[FunctionVersionLatestPublished], default: None ) –

    Specifies where to publish the function version or configuration.

  • dry_run (Optional[Boolean], default: None ) –

    Set to true to validate the request parameters and access permissions without modifying the function code.

  • revision_id (Optional[String], default: None ) –

    Update the function only if the revision ID matches the ID that's specified. Use this option to avoid modifying a function that has changed since you last read it.

  • source_kms_key_arn (Optional[KMSKeyArn], default: None ) –

    The ARN of the Key Management Service (KMS) customer managed key that's used to encrypt your function's .zip deployment package. If you don't provide a customer managed key, Lambda uses an Amazon Web Services managed key.

Raises:

Examples:

To update a Lambda function's code The following example replaces the code of the unpublished ($LATEST) version of a function named my-function with the contents of the specified zip file in Amazon S3.

>>> client.update_function_code(function_name='my-function', s3_bucket='my-bucket-1xpuxmplzrlbh', s3_key='function.zip')
Source code in docs_src/capo_lambda/_services/_lambda.py
def update_function_code(
    self,
    function_name: "capo_lambda.types.function_name.FunctionName",
    *,
    config_overrides: Optional[LambdaClientConfig] = None,
    zip_file: Optional["capo_lambda.types.blob.Blob"] = None,
    s3_bucket: Optional["capo_lambda.types.s3_bucket.S3Bucket"] = None,
    s3_key: Optional["capo_lambda.types.s3_key.S3Key"] = None,
    s3_object_version: Optional[
        "capo_lambda.types.s3_object_version.S3ObjectVersion"
    ] = None,
    s3_object_storage_mode: Optional[
        "capo_lambda.types.s3_object_storage_mode.S3ObjectStorageMode"
    ] = None,
    image_uri: Optional["capo_lambda.types.string.String"] = None,
    architectures: Optional[
        "capo_lambda.types.architectures_list.ArchitecturesList"
    ] = None,
    publish: Optional["capo_lambda.types.boolean.Boolean"] = None,
    publish_to: Optional[
        "capo_lambda.types.function_version_latest_published.FunctionVersionLatestPublished"
    ] = None,
    dry_run: Optional["capo_lambda.types.boolean.Boolean"] = None,
    revision_id: Optional["capo_lambda.types.string.String"] = None,
    source_kms_key_arn: Optional["capo_lambda.types.kms_key_arn.KMSKeyArn"] = None,
) -> "capo_lambda.types.function_configuration.FunctionConfiguration":
    """<p>Updates a Lambda Event Function's code. If code signing is enabled for the function, the code package must be signed by a trusted publisher. For more information, see <a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-codesigning.html">Configuring code signing for Lambda</a>.</p> <p>If the function's package type is <code>Image</code>, then you must specify the code package in <code>ImageUri</code> as the URI of a <a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-images.html">container image</a> in the Amazon ECR registry.</p> <p>If the function's package type is <code>Zip</code>, then you must specify the deployment package as a <a href="https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-package.html#gettingstarted-package-zip">.zip file archive</a>. Enter the Amazon S3 bucket and key of the code .zip file location. You can also provide the function code inline using the <code>ZipFile</code> field.</p> <p>The code in the deployment package must be compatible with the target instruction set architecture of the function (<code>x86-64</code> or <code>arm64</code>).</p> <p>The function's code is locked when you publish a version. You can't modify the code of a published version, only the unpublished version.</p> <note> <p>For a function defined as a container image, Lambda resolves the image tag to an image digest. In Amazon ECR, if you update the image tag to a new image, Lambda does not automatically update the function.</p> </note>

    Args:
        function_name: <p>The name or ARN of the Lambda function.</p> <p class="title"> <b>Name formats</b> </p> <ul> <li> <p> <b>Function name</b> – <code>my-function</code>.</p> </li> <li> <p> <b>Function ARN</b> – <code>arn:aws:lambda:us-west-2:123456789012:function:my-function</code>.</p> </li> <li> <p> <b>Partial ARN</b> – <code>123456789012:function:my-function</code>.</p> </li> </ul> <p>The length constraint applies only to the full ARN. If you specify only the function name, it is limited to 64 characters in length.</p>
        zip_file: <p>The base64-encoded contents of the deployment package. Amazon Web Services SDK and CLI clients handle the encoding for you. Use only with a function defined with a .zip file archive deployment package.</p>
        s3_bucket: <p>An Amazon S3 bucket in the same Amazon Web Services Region as your function. The bucket can be in a different Amazon Web Services account. Use only with a function defined with a .zip file archive deployment package.</p>
        s3_key: <p>The Amazon S3 key of the deployment package. Use only with a function defined with a .zip file archive deployment package.</p>
        s3_object_version: <p>For versioned objects, the version of the deployment package object to use.</p>
        s3_object_storage_mode: <p>Specifies how the deployment package is stored. Valid values:</p> <ul> <li> <p> <code>COPY</code> (default) – Uploads a copy of your deployment package to Lambda.</p> </li> <li> <p> <code>REFERENCE</code> – Lambda references the deployment package from the specified Amazon S3 bucket.</p> </li> </ul>
        image_uri: <p>URI of a container image in the Amazon ECR registry. Do not use for a function defined with a .zip file archive.</p>
        architectures: <p>The instruction set architecture that the function supports. Enter a string array with one of the valid values (arm64 or x86_64). The default value is <code>x86_64</code>.</p>
        publish: <p>Set to true to publish a new version of the function after updating the code. This has the same effect as calling <a>PublishVersion</a> separately.</p>
        publish_to: <p>Specifies where to publish the function version or configuration.</p>
        dry_run: <p>Set to true to validate the request parameters and access permissions without modifying the function code.</p>
        revision_id: <p>Update the function only if the revision ID matches the ID that's specified. Use this option to avoid modifying a function that has changed since you last read it.</p>
        source_kms_key_arn: <p>The ARN of the Key Management Service (KMS) customer managed key that's used to encrypt your function's .zip deployment package. If you don't provide a customer managed key, Lambda uses an Amazon Web Services managed key.</p>

    Raises:
        capo_lambda.errors.code_signing_config_not_found_exception.CodeSigningConfigNotFoundException: <p>The specified code signing configuration does not exist.</p>
        capo_lambda.errors.code_storage_exceeded_exception.CodeStorageExceededException: <p>Your Amazon Web Services account has exceeded its maximum total code size. For more information, see <a href="https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html">Lambda quotas</a>.</p>
        capo_lambda.errors.code_verification_failed_exception.CodeVerificationFailedException: <p>The code signature failed one or more of the validation checks for signature mismatch or expiry, and the code signing policy is set to ENFORCE. Lambda blocks the deployment.</p>
        capo_lambda.errors.invalid_code_signature_exception.InvalidCodeSignatureException: <p>The code signature failed the integrity check. If the integrity check fails, then Lambda blocks deployment, even if the code signing policy is set to WARN.</p>
        capo_lambda.errors.invalid_parameter_value_exception.InvalidParameterValueException: <p>One of the parameters in the request is not valid.</p>
        capo_lambda.errors.precondition_failed_exception.PreconditionFailedException: <p>The RevisionId provided does not match the latest RevisionId for the Lambda function or alias.</p> <ul> <li> <p> <b>For AddPermission and RemovePermission API operations:</b> Call <code>GetPolicy</code> to retrieve the latest RevisionId for your resource.</p> </li> <li> <p> <b>For all other API operations:</b> Call <code>GetFunction</code> or <code>GetAlias</code> to retrieve the latest RevisionId for your resource.</p> </li> </ul>
        capo_lambda.errors.resource_conflict_exception.ResourceConflictException: <p>The resource already exists, or another operation is in progress.</p>
        capo_lambda.errors.resource_not_found_exception.ResourceNotFoundException: <p>The resource specified in the request does not exist.</p>
        capo_lambda.errors.service_exception.ServiceException: <p>The Lambda service encountered an internal error.</p>
        capo_lambda.errors.too_many_requests_exception.TooManyRequestsException: <p>The request throughput limit was exceeded. For more information, see <a href="https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html#api-requests">Lambda quotas</a>.</p>
        capo_lambda.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To update a Lambda function's code
        The following example replaces the code of the unpublished ($LATEST) version of a function named my-function with the contents of the specified zip file in Amazon S3.

        >>> client.update_function_code(function_name='my-function', s3_bucket='my-bucket-1xpuxmplzrlbh', s3_key='function.zip')
    """

    def _handler(
        req: "OperationRequest[capo_lambda.types.update_function_code_request.UpdateFunctionCodeRequest]",
    ) -> OperationResponse[
        "capo_lambda.types.function_configuration.FunctionConfiguration"
    ]:
        import capo_lambda._operations.aws_gir_api_service.update_function_code

        output, http_response = (
            capo_lambda._operations.aws_gir_api_service.update_function_code.update_function_code(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_lambda.types.update_function_code_request.UpdateFunctionCodeRequest",
    ) -> dict[str, Any]:
        import capo_lambda.types.update_function_code_request

        return capo_lambda.types.update_function_code_request.redact_sensitive(
            input_
        )

    def _redact_output(
        output: "capo_lambda.types.function_configuration.FunctionConfiguration",
    ) -> dict[str, Any]:
        import capo_lambda.types.function_configuration

        return capo_lambda.types.function_configuration.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "UpdateFunctionCode",
            redact_input=_redact_input,
            redact_output=_redact_output,
        ),
    )
    input_: capo_lambda.types.update_function_code_request.UpdateFunctionCodeRequest = {
        "function_name": function_name
    }
    if zip_file is not None:
        input_["zip_file"] = zip_file
    if s3_bucket is not None:
        input_["s3_bucket"] = s3_bucket
    if s3_key is not None:
        input_["s3_key"] = s3_key
    if s3_object_version is not None:
        input_["s3_object_version"] = s3_object_version
    if s3_object_storage_mode is not None:
        input_["s3_object_storage_mode"] = s3_object_storage_mode
    if image_uri is not None:
        input_["image_uri"] = image_uri
    if architectures is not None:
        input_["architectures"] = architectures
    if publish is not None:
        input_["publish"] = publish
    if publish_to is not None:
        input_["publish_to"] = publish_to
    if dry_run is not None:
        input_["dry_run"] = dry_run
    if revision_id is not None:
        input_["revision_id"] = revision_id
    if source_kms_key_arn is not None:
        input_["source_kms_key_arn"] = source_kms_key_arn

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output