📝

Lambda の各ランタイムで AWS SDK のバージョンを確認する方法

に公開

以下のランタイムで確認してみました。

  • Node.js
  • Python
  • Ruby
  • Java
  • .NET 8 (C#)

Node.js

exports.handler = async (event) => {
  const { S3Client } = require("@aws-sdk/client-s3");
  const s3Package = require("@aws-sdk/client-s3/package.json");
  console.log(s3Package.version);
};

実行結果

3.806.0

Python

import boto3

def lambda_handler(event, context):
    boto3_version = boto3.__version__
    print(boto3_version)

実行結果

1.38.36

Ruby

require 'aws-sdk-core'

def lambda_handler(event:, context:)
  version = Gem.loaded_specs['aws-sdk-core']&.version&.to_s
  puts version
end

実行結果

3.226.0

Java

CloudShell から Java の Lambda 関数を作成してみた

package com.example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.util.VersionInfoUtils;

public class HelloWorld implements RequestHandler<Object, String> {
    @Override
    public String handleRequest(Object input, Context context) {
        String sdkVersion = VersionInfoUtils.getVersion();
        System.out.println(sdkVersion);
        return sdkVersion;
    }
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>lambda-hello-world</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>lambda-hello-world</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.2.0</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-core</artifactId>
      <version>1.12.568</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

実行結果

1.12.568

.NET 8 (C#)

AWS CloudShell から .NET の Lambda 関数をデプロイしてみた
ビルド前に以下のコマンドも実行しておきます。

$ dotnet add package AWSSDK.Core
using Amazon.Lambda.Core;
using System.Reflection;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace HelloWorldLambda;

public class Function
{
    public string FunctionHandler(string input, ILambdaContext context)
    {
        var awsSdkVersion = Assembly.GetAssembly(typeof(Amazon.AWSConfigs))?.GetName().Version?.ToString() ?? "Unknown";
        
        context.Logger.LogInformation(awsSdkVersion);
        
        return awsSdkVersion;
    }
}

実行結果

4.0.0.0

まとめ

今回は Lambda の各ランタイムで AWS SDK のバージョンを確認する方法を紹介しました。
どなたかの参考になれば幸いです。

Discussion