|
| 1 | +require 'json' |
| 2 | +require 'net/http' |
| 3 | +require 'uri' |
| 4 | +require 'time' |
| 5 | + |
| 6 | +def lambda_handler(event:, context:) |
| 7 | + detail = event['detail'] |
| 8 | + pipeline = detail['pipeline'] |
| 9 | + stage = detail['stage'] |
| 10 | + state = detail['state'] |
| 11 | + region = event['region'] |
| 12 | + |
| 13 | + kst_time = Time.now.getlocal('+09:00').strftime("%Y-%m-%d %H:%M:%S KST") |
| 14 | + |
| 15 | + color = case state |
| 16 | + when 'Succeeded' then 0x2ECC71 # 초록 |
| 17 | + when 'Failed' then 0xE74C3C # 빨강 |
| 18 | + when 'InProgress'then 0x3498DB # 파랑 |
| 19 | + else 0x95A5A6 # 회색 |
| 20 | + end |
| 21 | + |
| 22 | + embed = { |
| 23 | + title: "CodePipeline Stage 업데이트", |
| 24 | + description: "**Pipeline:** `#{pipeline}`\n" \ |
| 25 | + "**Stage:** `#{stage}`\n" \ |
| 26 | + "**State:** `#{state}`", |
| 27 | + color: color, |
| 28 | + footer: { |
| 29 | + text: "AWS Region: #{region} | #{kst_time}" |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + webhook_url = ENV['DISCORD_WEBHOOK_URL'] |
| 34 | + uri = URI.parse(webhook_url) |
| 35 | + |
| 36 | + header = { 'Content-Type': 'application/json' } |
| 37 | + payload = { |
| 38 | + content:"<@&1315864971426529281>, |
| 39 | + username: "AWS CodePipeline Notifier", |
| 40 | + avatar_url: "https://a0.awsstatic.com/libra-css/images/logos/aws_logo_smile_1200x630.png", |
| 41 | + embeds: [embed] |
| 42 | + } |
| 43 | + |
| 44 | + http = Net::HTTP.new(uri.host, uri.port) |
| 45 | + http.use_ssl = true |
| 46 | + request = Net::HTTP::Post.new(uri.request_uri, header) |
| 47 | + request.body = payload.to_json |
| 48 | + |
| 49 | + response = http.request(request) |
| 50 | + puts "Discord 응답 코드: #{response.code}" |
| 51 | + |
| 52 | + { statusCode: 200, body: 'Success' } |
| 53 | +end |
| 54 | + |
0 commit comments