3

I am trying to build AWS SDK for c++ with static linking, so I can use it as a binary inside AWS Lambda function.

Steps which I took are as follows:

  1. git clone https://github.com/aws/aws-sdk-cpp.git.
  2. mkdir build && cd build
  3. cmake .. -DBUILD_SHARED_LIBS=OFF -DBUILD_ONLY="s3" -DENABLE_TESTING=OFF -DFORCE_SHARED_CRT=OFF (which created libaws-cpp-sdk-s3.a inside aws-cpp-sdk-s3 directory)
  4. Now my source CMakeLists.txt looks like below
cmake_minimum_required(VERSION 3.1)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_BUILD_TYPE "Release")
project(executable LANGUAGES CXX)

add_executable(${PROJECT_NAME} "execute_code.cpp")

set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::Crypto)

set(aws-sdk-cpp_DIR "<path to aws-sdk-cpp/build>")
find_package(aws-sdk-cpp)

link_libraries(aws-cpp-sdk-core)

target_link_libraries(executable PRIVATE aws-cpp-sdk-s3 aws-cpp-sdk-core)

target_compile_features(${PROJECT_NAME} PRIVATE "cxx_std_11")
target_compile_options(${PROJECT_NAME} PRIVATE
    "-Wall"
    "-Wextra"
    "-Wconversion"
    "-Wshadow"
    "-Wno-sign-conversion")
#set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++")
include_directories(${PROJECT_SOURCE_DIR})

I am aware that AWS SDK for c++ requires OpenSSL and I have added that inside CMakeLists and when I run make after Cmake command, tt shows

-- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.a (found version "1.0.2g") 

so project is able to find static libcrypto. But when I am deploying it on AWS lambda, it gives error error while loading shared libraries: libcrypto.so.1.0.0: cannot open shared object file: No such file or directory\n.

Can anyone tell me how do I debug this or If I am missing anything? I tried searching for it, but couldn't find anything useful for static linking.

Here is my execute_code.cpp:

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/Bucket.h>
#include<iostream>

int main(int argc, char** argv) {

  Aws::SDKOptions options;
  Aws::InitAPI(options);
  {
      // snippet-start:[s3.cpp.list_buckets.code]
      Aws::S3::S3Client s3_client;
      auto outcome = s3_client.ListBuckets();

      if (outcome.IsSuccess())
      {
          std::cout << "Your Amazon S3 buckets:" << std::endl;

          Aws::Vector<Aws::S3::Model::Bucket> bucket_list =
              outcome.GetResult().GetBuckets();

          for (auto const &bucket : bucket_list)
          {
              std::cout << "  * " << bucket.GetName() << std::endl;
          }
      }
      else
      {
          std::cout << "ListBuckets error: "
              << outcome.GetError().GetExceptionName() << " - "
              << outcome.GetError().GetMessage() << std::endl;
      }
      // snippet-end:[s3.cpp.list_buckets.code]
  }
  Aws::ShutdownAPI(options);

It will also be helpful if anyone can tell how can I deploy shared libraries on AWS lambda.

Edit: I was able to solve this by building on Amazon Linux instead of Ubuntu machine.

ashutosh sharma
  • 192
  • 2
  • 8

0 Answers0