在Docker中未访问AWS Config凭证

时间:2020-07-09 18:11:02

标签: node.js amazon-web-services docker aws-secrets-manager

我在本地系统上设置了秘密管理器,现在在Windows根目录中有.aws目录。通过使用以下代码,我将获取我的access-key-id和value。

client.getSecretValue({SecretId: secretName}, function(err, data) {
// console.log(err);
if (err) {
    console.log(err);
    if (err.code === 'DecryptionFailureException')
        // Secrets Manager can't decrypt the protected secret text using the provided KMS key.
        throw err;
    else if (err.code === 'InternalServiceErrorException')
        // An error occurred on the server side.
        throw err;
    else if (err.code === 'InvalidParameterException')
        // You provided an invalid value for a parameter.
        throw err;
    else if (err.code === 'InvalidRequestException')
        // You provided a parameter value that is not valid for the current state of the resource.
        throw err;
    else if (err.code === 'ResourceNotFoundException')
        // We can't find the resource that you asked for.
        throw err;
}
else {
    
    // Decrypts secret using the associated KMS CMK.
    // Depending on whether the secret is a string or binary, one of these fields will be populated.
    if ('SecretString' in data) {
        secret = JSON.parse(data.SecretString);
        secretKey = secret["AWS_ACCESS_KEY_ID"];
        clientID = secret["AWS_ACCESS_KEY_ID"];
        secret.region = "us-east-1";
        global.secret = secret;
    } else {
        let buff = new Buffer(data.SecretBinary, 'base64');
        decodedBinarySecret = buff.toString('ascii');
    }

    // routes 
   
    require('./services')(router,validation);
}

});

运行良好。但是当我用docker运行上面的代码时,它失败并显示以下错误

配置中缺少凭据,如果使用AWS_CONFIG_FILE,则设置AWS_SDK_LOAD_CONFIG = 1。

1 个答案:

答案 0 :(得分:0)

您可以通过CLI以environment variables的形式传递这些值。

为此,请使用以下语法运行

docker run -it -p 3000:3000 -e AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE -e AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY service-docker-image-2:latest .