LoginSignup
11
10

More than 5 years have passed since last update.

AWS SDK for PHP 2をインストールして、サンプルコードを動かす

Last updated at Posted at 2015-04-28

まずはS3にアクセスできるIAM Roleを作る

あとで、EC2の自動起動とCloudWatch Logを利用したいので、その権限もここで合わせて作成しちゃう
与える権限はEC2フルアクセスとS3フルアクセスとCloudWatchログフルアクセス

  1. サービス→IAM→ロール→新しいロールの作成
  2. ロール名にAutoStart入れる→ロールタイプの選択EC2→AmazonS3FullAccessとAmazonEC2FullAccess→ロール作成
  3. ロールAutoStartをクリック→インラインポリシー作成→カスタムポリシーCloudWatchLog-FullAccess
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:*"
            ],
            "Resource": [
                "arn:aws:logs:*:*:*"
            ]
        }
    ]
}

IAM Roleを適用してEC2を立ち上げる。

  • 作成時のインスタンスの詳細の設定の時にIAM ロールでAutoStartを選択して立ち上げる。
  • userdataを使用して基本的な設定を入れておく→EC2 User DataでLAMP環境を作成する
  • mysqlとhttpd、その他不要なのは外して適用するのがいいかも。

AWS SDK for PHP 2をインストールする

サンプルコードダウンロード

$ git clone https://github.com/awslabs/aws-php-sample.git
Cloning into 'aws-php-sample'...
remote: Counting objects: 77, done.
remote: Total 77 (delta 0), reused 0 (delta 0), pack-reused 77
Unpacking objects: 100% (77/77), done.
Checking connectivity... done.

composerを使えるようにする。

$ cd aws-php-sample/
$ curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /home/ec2-user/aws-php-sample/composer.phar
Use it: php composer.phar

composerでAWS SDK for php2をインストールする

composer.jsonにすでにインストールするVersionとか書いてある。

$ php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing symfony/event-dispatcher (v2.6.6)
    Downloading: 100%

  - Installing guzzle/guzzle (v3.9.3)
    Downloading: 100%

  - Installing aws/aws-sdk-php (2.6.16)
    Downloading: 100%

symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/event-dispatcher suggests installing symfony/http-kernel ()
guzzle/guzzle suggests installing guzzlehttp/guzzle (Guzzle 5 has moved to a new package name. The package you have installed, Guzzle 3, is deprecated.)
aws/aws-sdk-php suggests installing doctrine/cache (Adds support for caching of credentials and responses)
aws/aws-sdk-php suggests installing ext-apc (Allows service description opcode caching, request and response caching, and credentials caching)
aws/aws-sdk-php suggests installing monolog/monolog (Adds support for logging HTTP requests and responses)
aws/aws-sdk-php suggests installing symfony/yaml (Eases the ability to write manifests for creating jobs in AWS Import/Export)
Writing lock file
Generating autoload files

サンプルプログラム実行

S3サンプルバケットを作って、ファイル保存して、最後にバケットごと消すプログラム。
実行前にphp.iniをdate.timezone = Asia/Tokyoと設定しておく

$ ls
LICENSE.txt  NOTICE.txt  README.md  composer.json  composer.lock  composer.phar  sample.php  vendor
$ php sample.php
Creating bucket named php-sdk-sample-553ef01bb49430.62451525
Creating a new object with key hello_world.txt
Downloading that same object:

---BEGIN---
Hello World!
---END---

Deleting object with key hello_world.txt
Deleting bucket php-sdk-sample-553ef01bb49430.62451525

Access Key と Cercret Access Keyはどう設定するの?

ベスト

IAM Roleを作って、EC2を起動する時にRoleを適用させる。
今回はRoleで AutoStartを適用した。

ベター

~/.aws/credentialsにキー、シークレットキーを設定する。

~/.aws/credentials
[default]
    aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
    aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY

[project1]
    aws_access_key_id = ANOTHER_AWS_ACCESS_KEY_ID
    aws_secret_access_key = ANOTHER_AWS_SECRET_ACCESS_KEY

危険

キーとシークレットキーをphpプログラムに記述する。
ファイルコピーや、git,svn等で簡単にキーが分散する。

start.php

<?php

use Aws\S3\S3Client;

// Instantiate the S3 client with your AWS credentials
$s3Client = S3Client::factory(array(
    'key'    => 'YOUR_AWS_ACCESS_KEY_ID',
    'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
));

次にやりたい事

EC2インスタンスを業務時間だけ自動起動する処理をAWS SDK for PHP 2で作成する

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10