github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/config/lambda.go (about)

     1  package config
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  )
     7  
     8  // defaultPolicy is the default function role policy.
     9  var defaultPolicy = IAMPolicyStatement{
    10  	"Effect":   "Allow",
    11  	"Resource": "*",
    12  	"Action": []string{
    13  		"logs:CreateLogGroup",
    14  		"logs:CreateLogStream",
    15  		"logs:PutLogEvents",
    16  		"ssm:GetParametersByPath",
    17  		"ec2:CreateNetworkInterface",
    18  		"ec2:DescribeNetworkInterfaces",
    19  		"ec2:DeleteNetworkInterface",
    20  	},
    21  }
    22  
    23  // IAMPolicyStatement configuration.
    24  type IAMPolicyStatement map[string]interface{}
    25  
    26  // VPC configuration.
    27  type VPC struct {
    28  	Subnets        []string `json:"subnets"`
    29  	SecurityGroups []string `json:"security_groups"`
    30  }
    31  
    32  // Lambda configuration.
    33  type Lambda struct {
    34  	// Memory of the function.
    35  	Memory int `json:"memory"`
    36  
    37  	// Timeout of the function.
    38  	Timeout int `json:"timeout"`
    39  
    40  	// Role of the function.
    41  	Role string `json:"role"`
    42  
    43  	// Runtime of the function.
    44  	Runtime string `json:"runtime"`
    45  
    46  	// Policy of the function role.
    47  	Policy []IAMPolicyStatement `json:"policy"`
    48  
    49  	// VPC configuration.
    50  	VPC *VPC `json:"vpc"`
    51  
    52  	// Accelerate enables S3 acceleration.
    53  	Accelerate bool `json:"accelerate"`
    54  
    55  	// Warm enables active warming.
    56  	Warm *bool `json:"warm"`
    57  
    58  	// WarmCount is the number of containers to keep active.
    59  	WarmCount int `json:"warm_count"`
    60  
    61  	// WarmRate is the schedule for performing worming.
    62  	WarmRate Duration `json:"warm_rate"`
    63  }
    64  
    65  // Default implementation.
    66  func (l *Lambda) Default() error {
    67  	if l.Memory == 0 {
    68  		l.Memory = 512
    69  	}
    70  
    71  	if l.Runtime == "" {
    72  		l.Runtime = "nodejs8.10"
    73  	}
    74  
    75  	l.Policy = append(l.Policy, defaultPolicy)
    76  
    77  	if l.WarmRate == 0 {
    78  		l.WarmRate = Duration(15 * time.Minute)
    79  	}
    80  
    81  	if l.WarmCount == 0 {
    82  		l.WarmCount = 15
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  // Validate implementation.
    89  func (l *Lambda) Validate() error {
    90  	if l.Timeout != 0 {
    91  		return errors.New(".lambda.timeout is deprecated, use .proxy.timeout")
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  // Override config.
    98  func (l *Lambda) Override(c *Config) {
    99  	if l.Memory != 0 {
   100  		c.Lambda.Memory = l.Memory
   101  	}
   102  
   103  	if l.Timeout != 0 {
   104  		c.Lambda.Timeout = l.Timeout
   105  	}
   106  
   107  	if l.Role != "" {
   108  		c.Lambda.Role = l.Role
   109  	}
   110  
   111  	if l.VPC != nil {
   112  		c.Lambda.VPC = l.VPC
   113  	}
   114  
   115  	if l.Warm != nil {
   116  		c.Lambda.Warm = l.Warm
   117  	}
   118  
   119  	if l.WarmCount > 0 {
   120  		c.Lambda.WarmCount = l.WarmCount
   121  	}
   122  
   123  	if l.WarmRate != 0 {
   124  		c.Lambda.WarmRate = l.WarmRate
   125  	}
   126  }