I'm quite new to AWS and docker.
Recently I got one task to store files (.json
) to aws s3 and to develop that I'm integrating LocalStack extension to simulate AWS s3 on my local machine.
I'm using .Net core 6 with visual studio 2022. also installed AWSToolkit and there I'm getting an error
Unable to connect to aws: The security token included in the request is invalid
inside docker-compose file, I've added S3 as a only value to SERVICES field. do I need any other services too there to configure S3, I'm not sure about that.
Docker code :
version: "3.7"
services:
localstack:
container_name: awslocal
image: localstack/localstack
hostname: awslocal
network_mode: bridge
ports:
- "4566:4566" # LocalStack Gateway
environment:
- SERVICES=s3
- DOCKER_HOST=unix:///var/run/docker.sock
- AWS_DEFAULT_REGION=eu-west-1
- AWS_ACCESS_KEY_ID=test
- AWS_SECRET_ACCESS_KEY=test
volumes:
- "${TEMPDIR:-/tmp/newFolder}:/tmp/newFolder"
- "/var/run/docker.sock:/var/run/docker.sock"
myApp:
image: ${DOCKER_REGISTRY-}myApp
network_mode: bridge
build:
context: .
dockerfile: myApp/Dockerfile
Here is the code snippet I'm using:
var config = new AmazonS3Config
{
ServiceURL = "http://127.0.0.1:4566",
ForcePathStyle = true,
};
var credentials = new BasicAWSCredentials("test", "test");
var s3Client = new AmazonS3Client(credentials, config);
var listObjectsRequest = new ListObjectsRequest
{
BucketName = "my-bucket"
};
var response = await s3Client.ListObjectsAsync(listObjectsRequest);
but I'm getting error as connection refused.
So far I've tried below things
Can anyone please help me with this? Thanks in advance :)
Hi — It appears to me that you’re accessing AWS resources such as S3 in LocalStack by running your application code in another container.
Your application container should be configured to use LocalStack as its DNS server. Once this is done, the domain name localhost.localstack.cloud
will resolve to the LocalStack container.
To configure your application container:
Here is a sample Docker Compose configuration that should give you a basic idea of how to set this up:
version: "3.8"
services:
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
image: localstack/localstack
ports:
# Now only required if you need to access LocalStack from the host
- "127.0.0.1:4566:4566"
# Now only required if you need to access LocalStack from the host
- "127.0.0.1:4510-4559:4510-4559"
environment:
- DEBUG=${DEBUG:-0}
volumes:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
networks:
ls:
# Set the container IP address in the 10.0.2.0/24 subnet
ipv4_address: 10.0.2.20
# sample application to showcase the example
application:
image: ghcr.io/localstack/localstack-docker-debug:main
entrypoint: ""
command: ["sleep", "infinity"]
dns:
# Set the DNS server to be the LocalStack container
- 10.0.2.20
networks:
- ls
networks:
ls:
ipam:
config:
# Specify the subnet range for IP address allocation
- subnet: 10.0.2.0/24
If you are using LocalStack, just for S3, please prefer to use
localstack/localstack:s3-latest
. Its a S3-compatible image with only S3 APIs, and is lightweight & faster compared to the original LocalStack image.