aws-doc-sdk-examples icon indicating copy to clipboard operation
aws-doc-sdk-examples copied to clipboard

Not able to import AWSSecretsManager in iOS project

Open JibuKThomas opened this issue 1 year ago • 9 comments

Expected behavior

I am trying to integrate AWSSecretsManager into an iOS application. I am using Swift package manager to integrate AWS swift sdk. But when we try to import the AWSSecretsManager we get an error "No such module AWSSecretsManager". Is there any solution for this issue

Actual behavior

User should be able to import the "AWSSecretsManager" after integrating the AWS swift sdk

Steps to reproduce

1.create a new project in Xcode  
2.Add the SDKs using swift package manager "https://github.com/awslabs/aws-sdk-swift"
3.import AWSSecretsManager in a viewcontroller and build the project. we will be getting an error "No such module AWSSecretsManager"

Logs / stacktrace (if applicable)

No response

Which SDK were you using?

Swift

Which OS were you using?

macOS

SDK version

No response

OS version

14.3.1

JibuKThomas avatar Feb 15 '24 13:02 JibuKThomas

This is curious. I can see the AWSSecretsManager files in the build directory, but you're right that importing it fails. I'm getting the SDK dev team involved.

shepazon avatar Feb 19 '24 17:02 shepazon

@JibuKThomas Did you add the AWSSecretManager module in the list of modules to be linked with your target ? There is a step missing in your list between 2. and 3.

2024-02-20_11-35-22

sebsto avatar Feb 20 '24 10:02 sebsto

Even with the target's dependencies updated, I still get the error. That is, my test project has this in its package file:

    targets: [
        .executableTarget(
            name: "TargetName",
            dependencies: [
                .product(name: "AWSSecretsManager", package: "aws-sdk-swift")
            ],
            path: "Sources"),
    ]

Yet the import AWSSecretsManager still fails.

shepazon avatar Feb 20 '24 15:02 shepazon

I just test this and it works. Note that this is an Xcode test, there is no Package.swift involved.

  1. Create a CLI project on Xcode

  2. Add AWS SDK Package dependency

image
  1. Cleanup the list of modules to link, be sure AWSSecretsManager is present
image
  1. write this code
import Foundation
import ClientRuntime
import AWSSecretsManager

@main
struct App {
    static func main() async throws {
        do {
            print("requesting secret")
            await SDKLoggingSystem.initialize(logLevel: .warning)
            let request = GetSecretValueInput(secretId: "ec2manager-amplify-app-id")
            let client = try AWSSecretsManager.SecretsManagerClient(region: "eu-central-1")
            let response = try await client.getSecretValue(input: request)
            print(response.secretString ?? "no response")
        } catch {
            print(error)
        }
    }
}

Works !

Output is

requesting secret
aaabbbcccc
Program ended with exit code: 0

sebsto avatar Feb 20 '24 15:02 sebsto

I also wrote a quick test with Package.swift (no xcode involved)

  1. Create a Swift project
➜  Desktop cd testsdk2 
➜  testsdk2 swift package init --type executable
Creating executable package: testsdk2
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
  1. Write Package.swift
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "testsdk2",
    platforms: [
        .macOS(.v13),
    ],
    dependencies: [
        .package(url: "https://github.com/awslabs/aws-sdk-swift.git", from: "0.36.0")
    ],
    targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .executableTarget(
            name: "testsdk2",
            dependencies: [
                .product(name: "AWSSecretsManager", package: "aws-sdk-swift")
            ]
        )
    ]
)
  1. write code
@main
struct App {
    static func main() async throws {
        do {
            print("requesting secret")
            await SDKLoggingSystem.initialize(logLevel: .warning)
            let request = GetSecretValueInput(secretId: "ec2manager-amplify-app-id")
            let client = try AWSSecretsManager.SecretsManagerClient(region: "eu-central-1")
            let response = try await client.getSecretValue(input: request)
            print(response.secretString ?? "no response")
        } catch {
            print(error)
        }
    }
}
  1. run and enjoy !
➜  testsdk2 swift run 
Building for debugging...
[562/562] Linking testsdk2
Build complete! (16.82s)
requesting secret
aaabbbccc

sebsto avatar Feb 20 '24 15:02 sebsto

I'm very confused why it doesn't work for me... strange.

shepazon avatar Feb 20 '24 15:02 shepazon

Oh. AWSSecretsManager isn't part of the AWS SDK for Swift at all. It's from the older AWS iOS SDK.

shepazon avatar May 06 '24 15:05 shepazon

Hello @shepazon, This is incorrect, AWSSecretsManager is part of the AWS SDK for Swift See this package https://github.com/awslabs/aws-sdk-swift/tree/main/Sources/Services/AWSSecretsManager

sebsto avatar May 06 '24 15:05 sebsto

Fah. I knew this. I'm just not used to seeing the client class references prefixed with the package name in the code; I would have written this line:

            let client = try AWSSecretsManager.SecretsManagerClient(region: "eu-central-1")

Instead as:

            let client = try SecretsManagerClient(region: "eu-central-1")

Sorry about that. I think we even went through this exact conversation once before. :)

shepazon avatar May 06 '24 18:05 shepazon