Skip to content

Instantly share code, notes, and snippets.

@charlieoleary
Last active May 14, 2019 19:31
Show Gist options
  • Save charlieoleary/2ee2a39e4ae63c1df3ca74ebf7846588 to your computer and use it in GitHub Desktop.
Save charlieoleary/2ee2a39e4ae63c1df3ca74ebf7846588 to your computer and use it in GitHub Desktop.
Login to AWS ECR with Chef + Docker Cookbook
#
# Cookbook:: utility
# Recipe:: ecr_auth
#
# depends 'docker', '~> 4.8.0'
#
# Required Attributes:
# default['aws']['ecr']['registry_id'] = AWS account ID for the registry.
# default['aws']['ecr']['region'] = AWS region for the registry.
#
# Your instance must also have an IAM role or credentials with appropriate policies configured.
package 'awscli'
Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
ruby_block 'get_erc_login' do
block do
credentials = shell_out("aws ecr get-login --no-include-email --registry-ids #{node['aws']['ecr']['registry_id']} --region #{node['aws']['ecr']['region']}")
node.default['aws']['ecr']['username'] = 'AWS'
node.default['aws']['ecr']['password'] = credentials.stdout.split[5]
node.default['aws']['ecr']['registry'] = credentials.stdout.split[6]
end
end
docker_registry "https://#{node['aws']['ecr']['registry_id']}.dkr.ecr.#{node['aws']['ecr']['region']}.amazonaws.com" do
username lazy { node['aws']['ecr']['username'] }
password lazy { node['aws']['ecr']['password'] }
email 'none'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment