create_function
¶
create_function(
function_name: FunctionName,
role: RoleArn,
code: FunctionCode,
*,
config_overrides: Optional[LambdaClientConfig] = None,
runtime: Optional[Runtime] = None,
handler: Optional[Handler] = None,
description: Optional[Description] = None,
timeout: Optional[Timeout] = None,
memory_size: Optional[MemorySize] = None,
publish: Optional[Boolean] = None,
publish_to: Optional[FunctionVersionLatestPublished] = None,
vpc_config: Optional[VpcConfig] = None,
package_type: Optional[PackageType] = None,
dead_letter_config: Optional[DeadLetterConfig] = None,
environment: Optional[Environment] = None,
kms_key_arn: Optional[KMSKeyArn] = None,
tracing_config: Optional[TracingConfig] = None,
tags: Optional[Tags] = None,
layers: Optional[LayerList] = None,
file_system_configs: Optional[FileSystemConfigList] = None,
code_signing_config_arn: Optional[CodeSigningConfigArn] = None,
image_config: Optional[ImageConfig] = None,
architectures: Optional[ArchitecturesList] = None,
ephemeral_storage: Optional[EphemeralStorage] = None,
snap_start: Optional[SnapStart] = None,
logging_config: Optional[LoggingConfig] = None,
tenancy_config: Optional[TenancyConfig] = None,
capacity_provider_config: Optional[CapacityProviderConfig] = None,
durable_config: Optional[DurableConfig] = None,
) -> FunctionConfiguration
Creates a Lambda Event Function. To create a function, you need a deployment package and an execution role. The deployment package is a .zip file archive or container image that contains your function code. The execution role grants the function permission to use Amazon Web Services services, such as Amazon CloudWatch Logs for log streaming and X-Ray for request tracing.
If the deployment package is a container image, then you set the package type to Image. For a container image, the code property must include the URI of a container image in the Amazon ECR registry. You do not need to specify the handler and runtime properties.
If the deployment package is a .zip file archive, then you set the package type to Zip. For a .zip file archive, the code property specifies the location of the .zip file. You must also specify the handler and runtime properties. The code in the deployment package must be compatible with the target instruction set architecture of the function (x86-64 or arm64). If you do not specify the architecture, then the default value is x86-64.
When you create a function, Lambda provisions an instance of the function and its supporting resources. If your function connects to a VPC, this process can take a minute or so. During this time, you can't invoke or modify the function. The State, StateReason, and StateReasonCode fields in the response from GetFunctionConfiguration indicate when the function is ready to invoke. For more information, see Lambda function states.
A function has an unpublished version, and can have published versions and aliases. The unpublished version changes when you update your function's code and configuration. A published version is a snapshot of your function code and configuration that can't be changed. An alias is a named resource that maps to a version, and can be changed to map to a different version. Use the Publish parameter to create version 1 of your function from its initial configuration.
The other parameters let you configure version-specific and function-level settings. You can modify version-specific settings later with UpdateFunctionConfiguration. Function-level settings apply to both the unpublished and published versions of the function, and include tags (TagResource) and per-function concurrency limits (PutFunctionConcurrency).
You can use code signing if your deployment package is a .zip file archive. To enable code signing for this function, specify the ARN of a code-signing configuration. When a user attempts to deploy a code package with UpdateFunctionCode, Lambda checks that the code package has a valid signature from a trusted publisher. The code-signing configuration includes set of signing profiles, which define the trusted publishers for this function.
If another Amazon Web Services account or an Amazon Web Services service invokes your function, use AddPermission to grant permission by creating a resource-based Identity and Access Management (IAM) policy. You can grant permissions at the function level, on a version, or on an alias.
To invoke your function directly, use Invoke. To invoke your function in response to events in other Amazon Web Services services, create an event source mapping (CreateEventSourceMapping), or configure a function trigger in the other service. For more information, see Invoking Lambda functions.
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.
-
-
runtime(Optional[Runtime], default:None) –The identifier of the function's runtime. Runtime is required if the deployment package is a .zip file archive. Specifying a runtime results in an error if you're deploying a function using a container image.
The following list includes deprecated runtimes. Lambda blocks creating new functions and updating existing functions shortly after each runtime is deprecated. For more information, see Runtime use after deprecation.
For a list of all currently supported runtimes, see Supported runtimes.
-
role(RoleArn) –The Amazon Resource Name (ARN) of the function's execution role.
-
handler(Optional[Handler], default:None) –The name of the method within your code that Lambda calls to run your function. Handler is required if the deployment package is a .zip file archive. The format includes the file name. It can also include namespaces and other qualifiers, depending on the runtime. For more information, see Lambda programming model.
-
code(FunctionCode) –The code for the function.
-
description(Optional[Description], default:None) –A description of the function.
-
timeout(Optional[Timeout], default:None) –The amount of time (in seconds) that Lambda allows a function to run before stopping it. The default is 3 seconds, and the maximum allowed value is 900 seconds. For functions using Lambda Managed Instances, asynchronous invocations and event source mapping invocations (except Amazon MQ and Amazon DocumentDB) support a maximum allowed value of 5,400 seconds (90 minutes). For more information, see Lambda execution environment.
-
memory_size(Optional[MemorySize], default:None) –The amount of memory available to the function at runtime. Increasing the function memory also increases its CPU allocation. The default value is 128 MB. The value can be any multiple of 1 MB.
-
publish(Optional[Boolean], default:None) –Set to true to publish the first version of the function during creation.
-
publish_to(Optional[FunctionVersionLatestPublished], default:None) –Specifies where to publish the function version or configuration.
-
vpc_config(Optional[VpcConfig], default:None) –For network connectivity to Amazon Web Services resources in a VPC, specify a list of security groups and subnets in the VPC. When you connect a function to a VPC, it can access resources and the internet only through that VPC. For more information, see Configuring a Lambda function to access resources in a VPC.
-
package_type(Optional[PackageType], default:None) –The type of deployment package. Set to
Imagefor container image and set toZipfor .zip file archive. -
dead_letter_config(Optional[DeadLetterConfig], default:None) –A dead-letter queue configuration that specifies the queue or topic where Lambda sends asynchronous events when they fail processing. For more information, see Dead-letter queues.
-
environment(Optional[Environment], default:None) –Environment variables that are accessible from function code during execution.
-
kms_key_arn(Optional[KMSKeyArn], default:None) –The ARN of the Key Management Service (KMS) customer managed key that's used to encrypt the following resources:
-
The function's environment variables.
-
The function's Lambda SnapStart snapshots.
-
When used with
SourceKMSKeyArn, the unzipped version of the .zip deployment package that's used for function invocations. For more information, see Specifying a customer managed key for Lambda. -
The optimized version of the container image that's used for function invocations. Note that this is not the same key that's used to protect your container image in the Amazon Elastic Container Registry (Amazon ECR). For more information, see Function lifecycle.
If you don't provide a customer managed key, Lambda uses an Amazon Web Services owned key or an Amazon Web Services managed key.
-
-
tracing_config(Optional[TracingConfig], default:None) –Set
ModetoActiveto sample and trace a subset of incoming requests with X-Ray. -
tags(Optional[Tags], default:None) –A list of tags to apply to the function.
-
layers(Optional[LayerList], default:None) –A list of function layers to add to the function's execution environment. Specify each layer by its ARN, including the version.
-
file_system_configs(Optional[FileSystemConfigList], default:None) –Connection settings for an Amazon EFS file system or an Amazon S3 Files file system.
-
code_signing_config_arn(Optional[CodeSigningConfigArn], default:None) –To enable code signing for this function, specify the ARN of a code-signing configuration. A code-signing configuration includes a set of signing profiles, which define the trusted publishers for this function.
-
image_config(Optional[ImageConfig], default:None) –Container image configuration values that override the values in the container image Dockerfile.
-
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. -
ephemeral_storage(Optional[EphemeralStorage], default:None) –The size of the function's
/tmpdirectory in MB. The default value is 512, but can be any whole number between 512 and 10,240 MB. For more information, see Configuring ephemeral storage (console). -
snap_start(Optional[SnapStart], default:None) –The function's SnapStart setting.
-
logging_config(Optional[LoggingConfig], default:None) –The function's Amazon CloudWatch Logs configuration settings.
-
tenancy_config(Optional[TenancyConfig], default:None) –Configuration for multi-tenant applications that use Lambda functions. Defines tenant isolation settings and resource allocations. Required for functions supporting multiple tenants.
-
capacity_provider_config(Optional[CapacityProviderConfig], default:None) –Configuration for the capacity provider that manages compute resources for Lambda functions.
-
durable_config(Optional[DurableConfig], default:None) –Configuration settings for durable functions. Enables creating functions with durability that can remember their state and continue execution even after interruptions.
Raises:
-
CodeSigningConfigNotFoundException–The specified code signing configuration does not exist.
-
CodeStorageExceededException–Your Amazon Web Services account has exceeded its maximum total code size. For more information, see Lambda quotas.
-
CodeVerificationFailedException–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.
-
FunctionVersionsPerCapacityProviderLimitExceededException–The maximum number of function versions that can be associated with a single capacity provider has been exceeded. For more information, see Lambda quotas.
-
InvalidCodeSignatureException–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.
-
InvalidParameterValueException–One of the parameters in the request is not valid.
-
ResourceConflictException–The resource already exists, or another operation is in progress.
-
ResourceNotFoundException–The resource specified in the request does not exist.
-
ServiceException–The Lambda service encountered an internal error.
-
TooManyRequestsException–The request throughput limit was exceeded. For more information, see Lambda quotas.
-
UnknownServiceError–The service returned an error code this client does not model.
Examples:
To create a function The following example creates a function with a deployment package in Amazon S3 and enables X-Ray tracing and environment variable encryption.
>>> client.create_function(code={'S3Bucket': 'my-bucket-1xpuxmplzrlbh', 'S3Key': 'function.zip'}, description='Process image objects from Amazon S3.', durable_config={'ExecutionTimeout': 31622400, 'RetentionPeriodInDays': 30}, environment={'Variables': {'BUCKET': 'my-bucket-1xpuxmplzrlbh', 'PREFIX': 'inbound'}}, function_name='my-function', handler='index.handler', kms_key_arn='arn:aws:kms:us-west-2:123456789012:key/b0844d6c-xmpl-4463-97a4-d49f50839966', memory_size=256, publish=True, role='arn:aws:iam::123456789012:role/lambda-role', runtime='nodejs12.x', tags={'DEPARTMENT': 'Assets'}, timeout=15, tracing_config={'Mode': 'Active'})
Source code in docs_src/capo_lambda/_services/_lambda.py
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 | |