1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from .. import Blueprint

from stacker.blueprints.variables.types import CFNString

from stacker_blueprints.aws_lambda import Function

from troposphere import awslambda

from ..policies import cloudwatch_logs_read_statements_datadog_log_forward_lambda


class LogForwardLambda(Function, Blueprint):

    OVERLOADED_VARIABLES = {
        "Code": {
            "type": int,
            "description": "Clobber / Override this Parameter.",
            "default": 0,
        },
        "DatadogAPIKey": {
            "type": CFNString,
            "no_echo": True
        },
        "HookDataKey": {
            "type": str,
            "description": "The data key to get the bucket/zip",
            "default": "lambda"
        },
    }

    def defined_variables(self):
        v = super(LogForwardLambda, self).defined_variables()
        v.update(self.OVERLOADED_VARIABLES)
        return v

    def code(self):
        variables = self.get_variables()
        key = variables["HookDataKey"]
        return self.with_resource_prefix("datadog-logs").context.hook_data[key]["Package"]

    def environment(self):
        variables = self.get_variables()
        variables["Environment"]["DD_API_KEY"] = variables["DatadogAPIKey"].value.strip()
        return awslambda.Environment(Variables=variables["Environment"])

    def generate_policy_statements(self):
        """Generates the policy statements for the role used by the function.
        Returns:
            list: A list of :class:`awacs.aws.Statement` objects.
        """
        statements = super(LogForwardLambda, self).generate_policy_statements()
        statements.extend(
            cloudwatch_logs_read_statements_datadog_log_forward_lambda()
        )
        return statements

    def create_template(self):
        super(LogForwardLambda, self).create_template()