blog-banner

AWS IAM for PHP

  • Aws
  • PHP

PHP on AWS

 

Amazon Web services have N number of services, one among them is Aws Identity and Access Management(IAM). IAM allows you to securely access the AWS services.

Using IAM you can control who can use your AWS service and what resources they can use. With IAM, you can create and manage AWS users and groups, and use permissions to allow and deny their access to AWS resources.

We can integrate IAM with many platforms like NodeJS, .NET, Java, etc., Here we can see how to use AWS IAM with the PHP platform.


require '/path/to/aws-autoloader.php';

<?php
$body  = array(‘type’ => ‘search’, ‘keyword’ => ‘phones’);
$credentials = new Aws\Credentials\Credentials(‘access_key’, ‘secret_key’);
//From https://github.com/aws/aws-sdk-php/issues/671#issuecomment-120666137
// Prepare a CloudSearchDomain request
$headers = array('x-api-key' => 'api_key, 'Content-Type => application/x-www-form-urlencoded');
$request = new GuzzleHttp\Psr7\Request('POST', 'https://www.knackforge.com/post-endpoint, ['Content-Type' => 'application/x-www-form-urlencoded'],  http_build_query($body, null, '&'));
// Construct a request signer
$signer = new Aws\Signature\SignatureV4('execute-api', 'us-west-2');
// Sign the request
$request = $signer->signRequest($request, $credentials);
// Send the request
$headers = array('Content-Type => application/x-www-form-urlencoded');
$client = new \GuzzleHttp\Client([ 'headers' => $headers]);
$response = $client->send($request);
$result = $response->getBody();

Installing Via ZIP

AWS SDK for PHP framework can be downloaded at https://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.zip

Installing via Composer

  1. Install Composer

    1. curl -sS https://getcomposer.org/installer | PHP

  2. Run the Composer command to install the latest stable version of the SDK:

    1. php composer.phar require aws/aws-sdk-php

  3. Require Composer's autoloader:

    1. <?php
      require 'vendor/autoload.php';

 

AWS IAM has lots of features some of which are

  • Shared access to your AWS account

  • Granular permissions

  • Secure access to AWS resources for applications that run on Amazon EC2

  • Multi-factor authentication (MFA)

  • Identity federation

  • Identity information for assurance

  • Integrated with many AWS services

  • Free to use