2022-09-19 12:00:09 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
Architecture ,
CreateFunctionCommand ,
LambdaClient ,
PackageType ,
Runtime ,
} from "@aws-sdk/client-lambda" ;
2024-10-16 11:53:18 -04:00
import { readFile } from "node:fs/promises" ;
2024-02-22 10:32:44 -05:00
import { dirnameFromMetaUrl } from "@aws-doc-sdk-examples/lib/utils/util-fs.js" ;
2022-09-19 12:00:09 -04:00
const dirname = dirnameFromMetaUrl ( import . meta . url ) ;
2022-09-19 14:05:31 -04:00
/** snippet-start:[javascript.v3.lambda.actions.CreateFunction] */
2022-09-19 12:00:09 -04:00
const createFunction = async ( funcName , roleArn ) => {
2023-10-11 11:30:41 -04:00
const client = new LambdaClient ( { } ) ;
2023-01-05 10:54:06 -05:00
const code = await readFile ( ` ${ dirname } ../functions/ ${ funcName } .zip ` ) ;
2022-09-19 12:00:09 -04:00
const command = new CreateFunctionCommand ( {
Code : { ZipFile : code } ,
FunctionName : funcName ,
Role : roleArn ,
Architectures : [ Architecture . arm64 ] ,
2022-09-27 09:18:01 -04:00
Handler : "index.handler" , // Required when sending a .zip file
PackageType : PackageType . Zip , // Required when sending a .zip file
Runtime : Runtime . nodejs16x , // Required when sending a .zip file
2022-09-19 12:00:09 -04:00
} ) ;
return client . send ( command ) ;
} ;
2022-09-19 14:05:31 -04:00
/** snippet-end:[javascript.v3.lambda.actions.CreateFunction] */
2022-09-19 12:00:09 -04:00
export { createFunction } ;