[mac] Middleman::S3Syncで使うAWS IAMユーザーのアクセスキー管理

2015.02.10

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Middleman S3Syncで使うIAMユーザーのアクセスキーをどのように管理するかについてメンバーに相談したところ、年齢詐称しているともっぱら噂の望月氏に教えてもらった方法がシンプルでわかりやすかったのでメモとして残しておきます。

必要なもの

筆者の環境ではrbenvとbundlerを使い、bundle execを省略できるような環境にしています。

用意するものは2つ。簡単です。Middlemanに関するその他の記事はこちらをどうぞ。

  • アクセスキーを保存しておくcredentialsファイル
  • AWS SDK for Ruby

アクセスキーを保存しておくファイルを作る

AWSのアクセスキーはuser rootに.aws/credentialsというファイルを作り、ここに保存しておくのが慣わしのようです。[...]でプロファイル名を指定します。このプロファイル名はconfig.rbからアクセスキーを呼び出すために必要です。

[s3-static-web-hogehoge]
aws_access_key_id = XXXXXXXXXXXXXXXXX
aws_secret_access_key = RjoiJksi2eualksIHK/9jhasSnj43ilkSFNU%KS345

[s3-static-web-hogehoge-stg]
aws_access_key_id = YYYYYYYYYYYYYYYYY
aws_secret_access_key = RjoiJksi2eualksIHK/9jhasSnj43ilkSFNU%KS345

試しに2つのプロファイルを用意していますが1つでも問題ありません。

AWS SDK for Ruby

config.rbでAWS SDK for Rubyを使ってアクセスキーを取得します。AWS SDK for RubyはGemをインストールするだけなのでとても簡単です。

Gemfileに追記

Gemfileにaws-sdk-v1を追記します。

gem 'aws-sdk-v1'

インストール

bundlerを使ってインストールします。

$ bundle install
Fetching gem metadata from https://rubygems.org/........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Using CFPropertyList 2.3.0
Using i18n 0.6.11
Using json 1.8.2
Using minitest 5.5.1
Using thread_safe 0.3.4
Using tzinfo 1.2.2
Using activesupport 4.1.9
Using addressable 2.3.7
Using ansi 1.4.3
Using mini_portile 0.6.2
Using nokogiri 1.6.6.2
Installing aws-sdk-v1 1.61.0
Using sass 3.4.11
Using thor 0.19.1
Using bourbon 4.2.0
Using builder 3.2.2
Using bundler 1.7.6
Using hitimes 1.2.2
Using timers 4.0.1
Using celluloid 0.16.0
Using chunky_png 1.3.3
Using coffee-script-source 1.9.0
Using execjs 2.3.0
Using coffee-script 2.3.0
Using multi_json 1.10.1
Using compass-core 1.0.3
Using compass-import-once 1.0.5
Using rb-fsevent 0.9.4
Using ffi 1.9.6
Using rb-inotify 0.9.5
Using compass 1.0.3
Using eventmachine 1.0.6
Using http_parser.rb 0.6.0
Using em-websocket 0.5.1
Using erubis 2.7.0
Using excon 0.44.1
Using exifr 1.2.0
Using fission 0.5.0
Using formatador 0.2.5
Using mime-types 2.4.3
Using net-ssh 2.9.2
Using net-scp 1.2.1
Using fog-core 1.28.0
Using fog-xml 0.1.1
Using fog-atmos 0.1.0
Using fog-json 1.0.0
Using ipaddress 0.8.0
Using fog-aws 0.1.0
Using inflecto 0.0.2
Using fog-brightbox 0.7.1
Using fog-ecloud 0.0.2
Using fog-profitbricks 0.0.1
Using fog-radosgw 0.0.3
Using fog-sakuracloud 1.0.0
Using fog-serverlove 0.1.1
Using fog-softlayer 0.4.0
Using fog-storm_on_demand 0.1.0
Using fog-terremark 0.0.3
Using fog-vmfusion 0.0.1
Using fog-voxel 0.0.2
Using fog 1.27.0
Using fspath 2.1.1
Using tilt 1.4.1
Using haml 4.0.6
Using hike 1.2.3
Using uber 0.0.13
Using hooks 0.4.0
Using htmlcompressor 0.1.2
Using image_size 1.4.1
Using in_threads 1.3.1
Using progress 3.1.0
Using image_optim 0.20.2
Using image_optim_pack 0.2.1.20150203
Using kramdown 1.5.0
Using listen 2.8.5
Using map 6.5.5
Using padrino-support 0.12.4
Using padrino-helpers 0.12.4
Using rack 1.6.0
Using rack-test 0.6.3
Using middleman-core 3.3.7
Using sprockets 2.12.3
Using sprockets-helpers 1.1.0
Using sprockets-sass 1.3.1
Using middleman-sprockets 3.4.1
Using uglifier 2.7.0
Using middleman 3.3.7
Using middleman-blog 3.5.3
Using middleman-imageoptim 0.2.0
Using rack-livereload 0.3.15
Using middleman-livereload 3.4.2
Using middleman-minify-html 3.4.0
Using pmap 1.0.2
Using ruby-progressbar 1.7.1
Using unf_ext 0.0.6
Using unf 0.1.4
Using middleman-s3_sync 3.0.41
Using neat 1.7.1
Your bundle is complete!
It was installed into ./vendor/bundle

これで準備は完了です。

config.rbでアクセスキーを取得する

config.rbに手を加えます。

AWS SDKの読み込み

まず最初にファイルの先頭でAWS SDKを読み込みます。

require 'aws-sdk-v1'

...

SDKを使ってアクセスキーを取得

S3Sync設定の部分にアクセスキーを取得するためのコードを追加します。profile_name = 's3-static-web-hogehoge'でプロファイル名を指定して、credential[:access_key_id]credential[:secret_access_key]でそれぞれのキーを渡します。

########################
#
# S3sync Setting
#
########################
activate :s3_sync do |config|

  # Profile name
  profile_name = 's3-static-web-hogehoge'
  # profile_name = 's3-static-web-hogehoge-stg' # <- staging

  # Get credential
  credential = AWS::Core::CredentialProviders::
                SharedCredentialFileProvider
                .new(profile_name: profile_name)
                .get_credentials

  # S3Sync config
  config.bucket                     = 'hogehoge.s3website.com'
  config.region                     = 'ap-northeast-1'
  config.aws_access_key_id          = credential[:access_key_id]
  config.aws_secret_access_key      = credential[:secret_access_key]
  config.delete                     = true
  config.after_build                = false
  config.prefer_gzip                = true
  config.path_style                 = true
  config.reduced_redundancy_storage = false
  config.acl                        = 'public-read'
  config.encryption                 = false
end

S3Syncを実行してみる

変更されたファイルはありませんが、無事Syncできているようです。

$ middleman s3_sync
== LiveReload accepting connections from http://192.168.1.47:35729
     s3_sync  Gathering the paths to evaluate.== LiveReload accepting connections from http://192.168.1.47:35729

Progress: |====================================================================================================================================================|
     s3_sync
All S3 files are up to date.

これで、環境変数も使うことなく、アクセスキーを充てることができるようになりました。安心してPublicなリポジトリにも置くことができますね。

この記事は初めてのMiddleman:Middleman::S3Syncで使うAWS IAMユーザーのアクセスキー管理方法についての転載です。

参考サイト