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

publish layer to multiple regions #23

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 41 additions & 27 deletions .github/workflows/fetch_release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@

ENV['AWS_REGION'] ||= 'us-east-1'

class Artifact
attr_reader :layer_version_arn
REGIONS = %w[
us-east-1
eu-west-2
]

class Artifact
def initialize(checksum:, path:, platform:)
@checksum = checksum
@path = path
Expand All @@ -41,7 +44,7 @@ def upload_github_asset(octokit, release)
puts asset.url
end

def upload_lambda_layer(aws)
def upload_lambda_layer(aws_clients)
return unless (lambda_arch = LAMBDA_PLATFORMS[@platform])

require 'zip'
Expand Down Expand Up @@ -71,28 +74,37 @@ def upload_lambda_layer(aws)
zip.add('extensions/skylight', list[0])
end

# 3 - make lambda layer
resp = aws.publish_layer_version({
compatible_architectures: [lambda_arch],
content: {
zip_file: File.open(zipfile_path, 'rb')
},
description: "Skylight #{VERSION}",
layer_name: "skylight-#{VERSION}-#{lambda_arch}".tr('.', '_'),
license_info: "MIT"
})

@layer_arn = resp.layer_arn
@layer_version_arn = resp.layer_version_arn
@layer_version = resp.version

permission_resp = aws.add_layer_version_permission({
action: "lambda:GetLayerVersion",
layer_name: @layer_arn,
principal: "*",
statement_id: "permission-#{VERSION}".tr('.', '_'),
version_number: @layer_version,
})
aws_clients.each do |client|

# 3 - make lambda layer
resp = client.publish_layer_version({
compatible_architectures: [lambda_arch],
content: {
zip_file: File.open(zipfile_path, 'rb')
},
description: "Skylight #{VERSION}",
layer_name: "skylight-#{VERSION}-#{lambda_arch}".tr('.', '_'),
license_info: "MIT"
})

layer_arn = resp.layer_arn
layer_version_arn = resp.layer_version_arn
layer_version = resp.version

permission_resp = client.add_layer_version_permission({
action: "lambda:GetLayerVersion",
layer_name: layer_arn,
principal: "*",
statement_id: "permission-#{VERSION}".tr('.', '_'),
version_number: layer_version,
})

layer_version_arns << layer_version_arn
end
end

def layer_version_arns
@layer_version_arns ||= []
end
end

Expand Down Expand Up @@ -135,7 +147,9 @@ def download_artifact(platform:, checksum:, version: VERSION, base_url: BASE_URL

LAMBDA_PLATFORMS = { 'x86_64-linux' => 'x86_64', 'aarch64-linux' => 'arm64' }.freeze

aws = Aws::Lambda::Client.new
aws = REGIONS.map do |region|
Aws::Lambda::Client.new(region: region)
end
# do this first to get the ARNs
artifacts.each do |artifact|
artifact.upload_lambda_layer(aws)
Expand All @@ -151,7 +165,7 @@ def download_artifact(platform:, checksum:, version: VERSION, base_url: BASE_URL
draft: true,
prerelease: true,
# TODO: better formatting
body: artifacts.map(&:layer_version_arn).compact.join("\n")
body: artifacts.map(&:layer_version_arns).flatten.join("\n")
)

puts "Release id #{release.id} tagged #{VERSION} (#{release.url})"
Expand Down
Loading