Partition of unity on neighborhood of compact set 
Let $U\subseteq\mathbb{R}^n$ be open and $D\subseteq U$ be compact. Prove that there is a $C^{\infty}$ function $f:\mathbb{R}^n\rightarrow\mathbb{R}$ such that $f$ takes on the value $1$ on a neighborhood of $D$, and the support of $f$ is contained in $U$.

There is a result that if $D$ is open and $U$ compact, then there exists an $\epsilon$-neighborhood of $D$ (let's call it $E$) that is contained in $U$. So $D\subseteq E\subseteq U$, and we can aim to have $f$ take on the value of $1$ in $E$.
We can take a $C^{\infty}$ partition of unity $\{\phi_i\}$ on $E$. So we have that $\phi_i\geq 0$ for all $x\in\mathbb{R}^n$, $\sum_{i=1}^{\infty}\phi_i(x)=1$ for all $x\in E$, and the support of $\phi_i$ is contained in $E$. I wonder if we can define the function $f(x)=\sum_{i=1}^{\infty}\phi_i(x)$. We will have $f(x)=1$ for all $x\in E$, but for $x\not\in E$, the sum $\sum_{i=1}^{\infty}\phi_i(x)$ might not converge. How can we fix this problem?
Edit: I think Etienne's solution that is put under "Edit" part almost works, except for a hole which I don't know how to fix. See my comment there.
 A: Here is the standard way to prove the result.
First note that there is a non-zero $\mathcal C^\infty$ function $\theta\geq 0$ on $\mathbb R$ which is supported on $[0,1]$.
From this, it follows that for any $\varepsilon >0$, there is a non-negative $\mathcal C^\infty$ function $\phi_\varepsilon$ on $\mathbb R^n$ which is supported on the closed euclidean ball $\overline B(0,\varepsilon)$ and such that $\int_{\mathbb R^n} \phi_\varepsilon (u) du=1$: just put $\phi_\varepsilon(x)= c_\varepsilon\, \theta\left(\frac{\Vert x\Vert^2}{\varepsilon^2}\right)$ for some suitably chosen constant $c_\varepsilon$.
Now, choose $\varepsilon>0$ such that $D+\overline B(0,2\varepsilon)\subset U$, and define $f$ to be the convolution $\mathbf 1_{D_\varepsilon}*\phi_\varepsilon$, where $D_\varepsilon=D+\overline B(0,\varepsilon)$:
$$f(x)=\int_{\mathbb R^n} \mathbf 1_{D_\varepsilon}(y)\phi_\varepsilon(x-y)\, dy\, . $$
Then, by the standard theorem on differentiation under the integral sign, $f$ is $\mathcal C^\infty$; and by a well known property of the support of a convolution, $${\rm supp}(f)\subset D_\varepsilon+{\rm supp}(\phi_\varepsilon)\subset D+\overline B(0, 2\varepsilon)\subset U\, .$$
Finally, $f$ is equal to $1$ on $D$. Indeed, write 
$$f(x)=\int_{\mathbb R^n} \mathbf 1_{D_\varepsilon}(x-y) \phi_\varepsilon (y)\, dy=
\int_{\overline B(0,\varepsilon)} \mathbf 1_{D_\varepsilon}(x-y) \phi_\varepsilon (y)\, dy $$
and observe that if $x\in D$, then $x-y\in D_\varepsilon$ for every $y\in\overline B(0,\varepsilon)$, i.e. $\mathbf 1_{D_\varepsilon}(x-y)=1$. It follows that for $x\in D$ we have
$$ f(x)=\int_{\overline B(0,\varepsilon)} \phi_\varepsilon (y)\, dy=\int_{\mathbb R^n} \phi_\varepsilon(y)\, dy=1\, .$$
$\bf Edit.$ If you want to find the function $f$ just by using the existence of a partition of unity as you stated it, you can do this assuming that your partition of unity $(\phi_i)_{i\in I}$ is relative to $U$ and is locally finite, i.e. each point $x\in\mathbb R^n$ has a neighbourhood $V_x$ on which all but finitely many functions $\phi_i$ are $0$. 
Assume that the closure of $E$ is contained in $U$. By compactness, you can cover $\overline E$ by finitely open sets $V_{x_1},\dots ,V_{x_N}$ as above; and moreover you may assume that $V_{x_j}\subset U$ for all $j$. For each $j\in\{ 1,\dots ,N\}$, choose a finite set $I_j\subset I$ such that $\phi_i\equiv 0$ on $V_{x_j}$ for all $i\not\in I_j$. Then let $I':=\bigcup_{j=1}^N I_j$ and $f:=\sum_{i\in I'} \phi_i$. The function $f$ is perfectly well-defined and $\mathcal C^\infty$ on $\mathbb R^n$ since this is a finite sum, and you do have $f\equiv 1$ on $E$ because $\phi_i\equiv 0$ on $\overline E$ for all $i\not\in I'$ and hence $f\equiv\sum_{i\in I}\phi_i=1$ on $E$.
A: There is a $C^\infty$ function $\phi:\ {\mathbb R}\to[0,1]$ with $\phi(t)=1$ for $t\leq1$ and $\phi(t)=0$ for $t\geq2$. For $\epsilon>0$ define $\phi_\epsilon:\ {\mathbb R}^n\to[0,1]$ by
$$\phi_\epsilon(x):=\phi\left({|x|\over\epsilon}\right)\qquad(x\in{\mathbb R}^n)\ .$$
The function $\phi_\epsilon$ is $\equiv1$ in the open $
\epsilon$-neighborhood $U_\epsilon(0)$  and $\equiv0$ outside  $U_{2\epsilon}(0)$.
Each point $x\in D$ has a neighborhood $U_{2\epsilon}(x)\subset U$, where $\epsilon >0$ depends on $x$. The family $\left(U_\epsilon(x)\right)_{x\in D}$ is an open covering of $D$; so there exists a finite set $\{x_1,x_2,\ldots, x_N\}\subset D$ such that writing $\epsilon(x_k)=:\epsilon_k$ one has
$$D\subset \bigcup\nolimits_{1\leq k\leq N} U_{\epsilon_k}(x_k)\ .$$
The $C^\infty$-function
$$\psi(x):=\prod_{k=1}^N\left(1-\phi_{\epsilon_k}(x-x_k)\right)$$
takes values in $[0,1]$, is $\equiv0$ on $D$, since for each $x\in D$ at least one $\phi_{\epsilon_k}(x-x_k)=1$, and is $\equiv1$ on ${\mathbb R}^n\setminus U$, since all $\phi_{\epsilon_k}(\cdot-x_k)$ vanish outside $U$.
It follows that $f(x):=1-\psi(x)$ has the required properties.
