Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not upload to S3 with ECS Fargate #1867

Closed
zeroiszero opened this issue Aug 16, 2019 · 5 comments
Closed

Could not upload to S3 with ECS Fargate #1867

zeroiszero opened this issue Aug 16, 2019 · 5 comments
Labels
guidance Question that needs advice or information.

Comments

@zeroiszero
Copy link

zeroiszero commented Aug 16, 2019

Hi, I am running WordPress with ECS Fargate and TaskRoleArn defined in Task Definition

Version of AWS SDK for PHP?

aws/aws-sdk-php": "~3.18"

Version of PHP (php -v)?

PHP 7.3.6 (cli) (built: Jun 12 2019 20:40:46) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.6, Copyright (c) 1998-2018 Zend Technologies

What issue did you see?

Fatal error: Uncaught GuzzleHttp\Exception\ConnectException: cURL error 7: (see http://curl.haxx.se/libcurl/c/libcurl-errors.html) in /var/www/html/wp-content/plugins/s3-uploads/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:185 Stack trace: #0 /var/www/html/wp-content/plugins/s3-uploads/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(149): GuzzleHttp\Handler\CurlFactory::createRejection(Object(GuzzleHttp\Handler\EasyHandle), Array) #1 /var/www/html/wp-content/plugins/s3-uploads/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(102): GuzzleHttp\Handler\CurlFactory::finishError(Object(GuzzleHttp\Handler\CurlMultiHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #2 /var/www/html/wp-content/plugins/s3-uploads/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(181): GuzzleHttp\Handler\CurlFactory::finish(Object(GuzzleHttp\Handler\CurlMultiHandler), Object(GuzzleHttp\Handler\EasyHandle), Object(GuzzleHttp\Handler\CurlFactory)) #3 /var/www/html/wp-content/plugins/s3-u in /var/www/html/wp-content/plugins/s3-uploads/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php on line 185

Steps to reproduce

Run S3 putObject as in this example.

Credentials not provided as I assume the SDK will retrieve from the IAM role.

Also tried the task role attached with the policy AmazonS3FullAccess. Still the same error.

@diehlaws diehlaws self-assigned this Aug 16, 2019
@diehlaws diehlaws added the guidance Question that needs advice or information. label Aug 16, 2019
@diehlaws
Copy link
Contributor

Hi @zeroiszero, thanks for reaching out to us. cURL Error 7 is caused by a failure to connect to the target host, which suggests there is something blocking or dropping the connection from your Fargate container to S3. Are you able to reach the S3 endpoint for the region in which your bucket resides using your container without using the AWS SDK for PHP? E.g. using curl or nc from the shell, or using fsockopen() or a GuzzleHttp client within PHP.

As a side note, I see you're using version 3.18 of the AWS SDK for PHP. While this shouldn't affect your ability to connect to S3, this version was released in April of 2016 and is 90 minor versions behind the latest release. I strongly suggest updating the SDK to a newer version as there have been quite a few bug fixes and improvements implemented between these two versions that should result in an overall better experience using the SDK.

@diehlaws diehlaws added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Aug 16, 2019
@zeroiszero
Copy link
Author

zeroiszero commented Aug 18, 2019

Hi, @diehlaws

Checked. No issue connect to S3 enpoint. Updated the SDK version as well, still the same.

As I traced the code, its turn out that cURL error is coming from the SDK when its trying to connect http://169.254.169.254/latest/meta-data/iam/security-credentials to retrieve the credentials / permissions from the IAM attached to Fargate task.

@zeroiszero
Copy link
Author

Solved.

It was caused by php-fpm configuration that deleted the environment variables when docker started php-fpm processes.

AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variable is required to retrieve credentials from Task Role as mentioned here.

To fix this, set clear_env to no in php-fpm configurations:

; Clear environment in FPM workers
; Prevents arbitrary environment variables from reaching FPM worker processes
; by clearing the environment in workers before env vars specified in this
; pool configuration are added.
; Setting to "no" will make all environment variables available to PHP code
; via getenv(), $_ENV and $_SERVER.
; Default Value: yes
clear_env = no

@diehlaws diehlaws removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Aug 19, 2019
@mvn-bachhuynh-dn
Copy link

hi @zeroiszero , I try like you did, but still error
aws/aws-sdk-php-laravel#176

@diehlaws diehlaws removed their assignment Aug 26, 2020
@Steven-Wright
Copy link

To fix this, set clear_env to no in php-fpm configurations:

I am so glad I found this post, I was really beginning to lose hope.

I'm not sure if it matters much, but rather than preventing FPM from clearing environment variables you could pass AWS_CONTAINER_CREDENTIALS_RELATIVE_URI specifically.

I added the line to /etc/php-fpm.d/www.conf:

env[AWS_CONTAINER_CREDENTIALS_RELATIVE_URI] = $AWS_CONTAINER_CREDENTIALS_RELATIVE_URI

Below the section:

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

4 participants