CLI 또는 SDK로 UpdateFunctionCode 사용
CLI 또는 SDK로 UpdateFunctionCode 사용 (Use UpdateFunctionCode with an AWS SDK or CLI)
다음 코드 예제는 UpdateFunctionCode을 사용해 기존 Lambda 함수의 코드를 업데이트하는 방법을 보여줍니다.
본문
CLI
AWS CLI
함수의 코드를 업데이트하려면:
다음 update-function-code 예제는 함수의 배포 패키지를 업데이트합니다.
aws lambda update-function-code \
--function-name my-function \
--zip-file fileb://function.zip
코드가 Amazon S3 버킷에 있는 경우 --s3-bucket, --s3-key, 선택적으로 --s3-object-version 옵션을 사용합니다.
aws lambda update-function-code \
--function-name my-function \
--s3-bucket amzn-s3-demo-bucket \
--s3-key function.zip \
--s3-object-version myObjectVersion
API 세부 정보는 AWS CLI 명령 참조의 UpdateFunctionCode을 참조하세요.
PowerShell
Tools for PowerShell V4:
예제 1: Lambda 함수의 코드를 업데이트합니다.
Update-LMFunctionCode -FunctionName "MylambdaFunction123" -ZipFile "C:\path\to\function.zip"
API 세부 정보는 AWS Tools for PowerShell Cmdlet Reference (V4)의 UpdateFunctionCode을 참조하세요.
Tools for PowerShell V5:
예제 1: Lambda 함수의 코드를 업데이트합니다.
Update-LMFunctionCode -FunctionName "MylambdaFunction123" -ZipFile "C:\path\to\function.zip"
API 세부 정보는 AWS Tools for PowerShell Cmdlet Reference (V5)의 UpdateFunctionCode을 참조하세요.
SDK for Java 2.x
다음 예제는 AWS SDK for Java 2.x로 Lambda 함수 코드를 업데이트하는 방법을 보여줍니다. 배포 패키지가 있는 Amazon S3 버킷과 키, 함수 이름, 런타임, 핸들러, 역할을 지정합니다.
import software.amazon.awssdk.services.lambda.LambdaClient;
import software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeRequest;
import software.amazon.awssdk.services.lambda.model.UpdateFunctionCodeResponse;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class UpdateFunction {
public static void main(String[] args) {
Region region = Region.US_WEST_2;
LambdaClient awsLambda = LambdaClient.builder().region(region).build();
String functionName = "my-function";
String filePath = "path/to/function.zip";
updateFunction(awsLambda, functionName, filePath);
awsLambda.close();
}
public static void updateFunction(LambdaClient awsLambda, String functionName, String filePath) {
try {
SdkBytes fileToUpload = SdkBytes.fromInputStream(Files.newInputStream(Paths.get(filePath)));
UpdateFunctionCodeRequest codeRequest = UpdateFunctionCodeRequest.builder()
.functionName(functionName)
.zipFile(fileToUpload)
.build();
UpdateFunctionCodeResponse response = awsLambda.updateFunctionCode(codeRequest);
System.out.println("The function was updated: " + response.functionName());
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}
SDK for JavaScript (v3)
다음 예제는 AWS SDK for JavaScript v3로 Lambda 함수 코드를 업데이트하는 방법을 보여줍니다.
import { UpdateFunctionCodeCommand, LambdaClient } from "@aws-sdk/client-lambda";
const client = new LambdaClient({ region: "us-west-2" });
const params = {
FunctionName: "my-function",
ZipFile: Buffer.from(fs.readFileSync("path/to/function.zip")),
};
const command = new UpdateFunctionCodeCommand(params);
const response = await client.send(command);
console.log(response);
SDK for Python (Boto3)
다음 예제는 AWS SDK for Python(Boto3)로 Lambda 함수 코드를 업데이트하는 방법을 보여줍니다.
import boto3
lambda_client = boto3.client('lambda')
with open('function.zip', 'rb') as f:
zipped_code = f.read()
response = lambda_client.update_function_code(
FunctionName='my-function',
ZipFile=zipped_code
)
print(response)
SDK for Go V2
다음 예제는 AWS SDK for Go V2로 Lambda 함수 코드를 업데이트하는 방법을 보여줍니다.
import (
"context"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda"
)
func updateFunction(client *lambda.Client, functionName string, filePath string) error {
content, err := os.ReadFile(filePath)
if err != nil {
return err
}
_, err = client.UpdateFunctionCode(context.TODO(), &lambda.UpdateFunctionCodeInput{
FunctionName: aws.String(functionName),
ZipFile: content,
})
return err
}
.NET
다음 예제는 AWS SDK for .NET으로 Lambda 함수 코드를 업데이트하는 방법을 보여줍니다.
using Amazon.Lambda;
using Amazon.Lambda.Model;
var client = new AmazonLambdaClient(Amazon.RegionEndpoint.USWest2);
var request = new UpdateFunctionCodeRequest
{
FunctionName = "my-function",
ZipFile = File.ReadAllBytes("path/to/function.zip")
};
var response = await client.UpdateFunctionCodeAsync(request);
Console.WriteLine(response.FunctionName);
Rust
다음 예제는 AWS SDK for Rust로 Lambda 함수 코드를 업데이트하는 방법을 보여줍니다.
use aws_sdk_lambda::Client;
use aws_sdk_lambda::primitives::Blob;
use std::fs;
async fn update_function(client: &Client, function_name: &str, file_path: &str) -> Result<(), aws_sdk_lambda::Error> {
let data = fs::read(file_path).unwrap();
client
.update_function_code()
.function_name(function_name)
.zip_file(Blob::new(data))
.send()
.await?;
Ok(())
}
PHP
다음 예제는 AWS SDK for PHP로 Lambda 함수 코드를 업데이트하는 방법을 보여줍니다.
use Aws\Lambda\LambdaClient;
$client = new LambdaClient([
'region' => 'us-west-2',
'version' => 'latest'
]);
$result = $client->updateFunctionCode([
'FunctionName' => 'my-function',
'ZipFile' => file_get_contents('path/to/function.zip')
]);
Ruby
다음 예제는 AWS SDK for Ruby로 Lambda 함수 코드를 업데이트하는 방법을 보여줍니다.
require 'aws-sdk-lambda'
client = Aws::Lambda::Client.new(region: 'us-west-2')
response = client.update_function_code(
function_name: 'my-function',
zip_file: File.binread('path/to/function.zip')
)
AWS SDK 개발자 안내서와 코드 예제의 전체 목록은 AWS SDK와 함께 Lambda 사용을 참조하세요. 이 주제에는 시작하기와 이전 SDK 버전에 대한 자세한 내용도 포함되어 있습니다.
더 알아보기 (Learn more)
- Lambda 배포 패키지 업데이트
- UpdateFunctionCode API
- AWS SDK와 함께 Lambda 사용