使用多个UserParameters从CodePipeline调用Lambda

时间:2017-01-26 16:35:24

标签: amazon-web-services aws-lambda amazon-cloudformation aws-codepipeline

本教程介绍如何从CodePipeline调用Lambda传递单个参数:

http://docs.aws.amazon.com/codepipeline/latest/userguide/how-to-lambda-integration.html

我已经构建了一个需要获得2个参数的slackhook lambda:

  • webhook_url
  • 消息

通过CodePipeline编辑器传入JSON会导致JSON块被发送,因此无法直接解析。

UserParameter传入:

{
  "webhook":"https://hooks.slack.com/services/T0311JJTE/3W...W7F2lvho",
  "message":"Staging build awaiting approval for production deploy"
}

事件有效负载中的用户参数

UserParameters: '{
  "webhook":"https://hooks.slack.com/services/T0311JJTE/3W...W7F2lvho",
  "message":"Staging build awaiting approval for production deploy"
}'

尝试在CLoudFormation中直接应用多个UserParameter时,如下所示:

Name: SlackNotification
  ActionTypeId:
    Category: Invoke
    Owner: AWS
    Version: '1'
    Provider: Lambda
  OutputArtifacts: []
  Configuration:
    FunctionName: aws-notify2
    UserParameters:
       - webhook: !Ref SlackHook
       - message: !Join [" ",[!Ref app, !Ref env, "build has started"]]
  RunOrder: 1

创建错误 - 配置必须只包含简单对象或字符串。

如何将多个UserParameter从CloudFormation模板传递到Lambda的任何猜测都将非常感激。

以下是lambda代码供参考: https://github.com/byu-oit-appdev/aws-codepipeline-lambda-slack-webhook

1 个答案:

答案 0 :(得分:6)

您应该能够将多个import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.Timer; public class Sprites { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frm1 = new JFrame(); Painel1 pn1 = new Painel1(); frm1.getContentPane().add(pn1); frm1.pack(); frm1.setVisible(true); frm1.setLocationRelativeTo(null); frm1.setResizable(false); frm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } } class Painel1 extends JPanel { int[][] spriteSheetCoords = { { 8, 10, 119, 129 }, { 138, 10, 118, 130 }, { 267, 10, 118, 132 }, { 402, 11, 113, 132 }, { 538, 12, 106, 134 }, { 671, 13, 103, 133 }, { 804, 12, 102, 132 }, { 23, 161, 100, 134 }, { 157, 162, 96, 134 }, { 287, 159, 95, 135 }, { 418, 158, 95, 133 }, { 545, 159, 99, 133 }, { 673, 159, 102, 134 }, { 798, 158, 108, 130 }, { 9, 309, 116, 126 }, { 137, 309, 118, 127 }, { 274, 310, 110, 128 }, { 412, 311, 102, 129 }, { 541, 312, 103, 130 }, { 671, 312, 104, 131 }, { 806, 312, 98, 132 }, { 29, 463, 94, 135 }, { 155, 462, 98, 135 }, { 279, 461, 104, 135 }, { 409, 461, 106, 135 }, { 536, 461, 109, 135 }, { 662, 461, 112, 133 } }; int i = 0; BufferedImage img; private ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { i++; if (i == spriteSheetCoords.length) { i = 0; } revalidate(); repaint(); } }; public Painel1() { Timer timer = new Timer(50, actionListener); timer.setInitialDelay(0); timer.start(); setBackground(Color.yellow); try { img = ImageIO.read(new File("/home/jesus/Pictures/tokyo.jpg")); } catch (IOException e) { e.printStackTrace(); } } @Override public void paintComponent(Graphics g) { Image subSprite; super.paintComponent(g); subSprite = img.getSubimage(spriteSheetCoords[i][0], spriteSheetCoords[i][1], spriteSheetCoords[i][2], spriteSheetCoords[i][3]); g.drawImage(subSprite, 140, 120, null); } @Override public Dimension getPreferredSize() { return new Dimension(400, 400); } } 作为单个JSON对象字符串传递,然后在收到时解析Lambda函数中的JSON。

这正是文档中Python example处理这种情况的方式:

Timer

同样,使用UserParameters在Node.JS中可以正常工作,将JSON对象字符串(如事件有效负载示例中所示)解析为可用的JSON对象:

try:
    # Get the user parameters which contain the stack, artifact and file settings
    user_parameters = job_data['actionConfiguration']['configuration']['UserParameters']
    decoded_parameters = json.loads(user_parameters)