3

I am unable to bring up my app. it always fails with missing credentials. How do I connect localstack s3 to my application. I've tried setting the args and running aws configure in my dockerfile, it still fails with missing credentials. I mounted the volume by copying my local credentials from .aws/credential file, but that is not ideal since i want localstack credentials set up. always failing with error unable to download: CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Dockerfile

FROM node:9.2

#install AWS CLI
RUN apt-get update && apt-get install -y python python-dev python-pip python-setuptools groff less && pip install awscli


WORKDIR /migration-ui

COPY migration-ui/package.json /migration-ui

RUN npm install

COPY migration-ui /migration-ui

EXPOSE 8080

CMD ["npm","start"]

docker compose

version: '3.7'
services:
  s3:
    image: localstack/localstack:latest
    container_name: 'localstack'
    ports:
      - '4563-4599:4563-4599'
      - '8082:8081'
    environment:
      - SERVICES=s3
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - './.localstack:/tmp/localstack'
      - '/var/run/docker.sock:/var/run/docker.sock'

  bmo-ui:
    depends_on:
      - s3
    build: .

s3.js

const s3Params = {
  Bucket: process.env.BMO_BUCKET || 'dev-csi-assets',
  Key: 'bmo-migration/bmo-migration-db.json'
}

const awsConfig = require('aws-config')
const AWS = require('aws-sdk')
const s3 = require('s3')

const awsContainerCredentialsRelativeUri = !!process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
console.log("-----ENVIRONMENTS----", awsContainerCredentialsRelativeUri)
console.log("VALUES-----", process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI)
const s3Options = {
  region: 'us-east-1',                 // explicitly set AWS region
  sslEnabled: true,                    // override whether SSL is enabled
  maxRetries: 3,                       // override the number of retries for a request
  profile: 'assumed_role',             // name of profile from ~/.aws/credentials
  timeout: 15000                       // optional timeout in ms. Will use AWS_TIMEOUT
}

let s3Client = new AWS.S3(awsConfig(s3Options))

if (awsContainerCredentialsRelativeUri) {
  AWS.config.credentials = new AWS.ECSCredentials()
  s3Client = new AWS.S3()
}

const client = s3.createClient({s3Client})

const download = (path, cb = () => {}) => {
  try {
    const params = {
      localFile: path,
      s3Params: s3Params
    }
    const downloader = client.downloadFile(params)
    downloader.on('end', () => {
      console.log('done downloading')
      cb()
    })
    downloader.on('error', err => {
      console.error('unable to download:', err.stack)
      cb(err)
    })
  } catch (e) {
    console.error(e)
    cb(e)
  }
}

const upload = (path, cb = () => {}) => {
  try {
    const params = {
      localFile: path,
      s3Params: s3Params
    }
    const uploader = client.uploadFile(params)
    uploader.on('error', err => {
      console.log('unable to upload:', err.stack)
      cb(err)
    })
    uploader.on('progress', () => {
      console.log('progress', uploader.progressMd5Amount, uploader.progressAmount, uploader.progressTotal)
    })
    uploader.on('end', () => {
      console.log('done uploading')
      cb()
    })
  } catch (e) {
    console.error(e)
    cb(e)
  }
}

module.exports = { download, upload }
1
  • thanks for accepting and updating the status of question. Commented Jul 8, 2021 at 14:50

1 Answer 1

1

Please try running image with environment variables

docker run \
        -e AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
        -e AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
        -e AWS_DEFAULT_REGION="$(REGION)" \
        "<Docker-Image>"

you can run container locally. You need to set the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION environment variables

it's working for me here in Makefile

https://github.com/harsh4870/cloud-custodian/blob/master/Makefile

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.